Android Application Development-sqliteopenhelper manages SQLite databases and listview

Source: Internet
Author: User

1. sqliteopenhelper introduction:

A helper class to manage database creation and version management.

That is to say, sqliteopenhelper is used to manage the versions used to create and manage a database.

In the class that inherits sqliteopenhelper (extends SQLiteOpenHelper), ThroughRewriteonCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,
int, int)
AndonOpen(SQLiteDatabase)(Optional.

Create a datebase helper class:

Public class databasehelper extends sqliteopenhelper {Private Static final int database_version = 1; string database_create = "create table user (_ id integer primary key autoincrement, mtext varchar (8000 ), mtime varchar (200) "; // constructor public databasehelper (context, string database_name) {super (context, database_name, null, database_version );} @ overridepublic void oncreate (sqlitedatabase dB) {// todo auto-generated method stubsystem. out. println ("create my idea. SQL "); // you can create two tables, db.exe csql (database_create); // important. This is called only when the database is created for the first time, you can also delete the created database before calling // db.exe csql (database_create1);} @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) again) {// todo auto-generated method stubsystem. out. println ("update a database ");}

2. perform database operations in activity:

// Add public void adddata () {contentvalues values = new contentvalues (); // values. put ("ID", 1); // The default _ id used to construct a table is used here, So values can be omitted in this sentence. put ("mtext", "name"); values. put ("mtime", "idea.com"); sqlitedatabase DB = dbhelper. getwritabledatabase (); long rowid = dB. insert ("user", null, values); // insert data in the table and return the _ idsystem. out. println ("-->" + rowid); // mlayout. postinvalidate (); dB. close () ;}// query data public void quer Y () {sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. query ("user", null, null); While (cursor. movetonext () {string mtext = cursor. getstring (cursor. getcolumnindex ("mtext"); string mtime = cursor. getstring (cursor. getcolumnindex ("mtime"); system. out. println ("" + cursor. getint (cursor. getcolumnindex ("_ id"); system. out. println ("query --->" + mtext + "" + Mt IME);} dB. close () ;}// update public void updatadata () {sqlitedatabase DB = dbhelper. getwritabledatabase (); contentvalues values = new contentvalues (); values. put ("mtext", "name1"); values. put ("mtime", "258963"); dB. update ("user", values, "_ id =? ", New string [] {" 4 "}); // update the data whose _ id is 4 // dB. update ("users", values, null, null); // update all the databases in the table. close () ;}// Delete the specified record public void deletedata () {sqlitedatabase DB = dbhelper. getreadabledatabase (); // delete all table data // dB. delete ("users", null, null); // delete a specified piece of data from the table db.exe csql ("delete from" + "user" + "where _ id =" + integer. tostring (2); dB. close () ;}// Delete the table or the whole database Public void deletetable () {sqlitedatabase DB = dbhelper. getreadabledatabase (); try {// Execute SQL sentence db.exe csql ("Drop table user"); // Delete table} catch (sqlexception e) {} This. deletedatabase ("idea. SQL "); // delete a database. If the database exists, enter the database name to delete the database. close ();}

3. listview operations

Main. xml

<ListView        android:paddingTop="30dp"        android:id="@+id/mlistview"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />

Listviewitem. xml

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:stretchColumns="1"    android:shrinkColumns="1,2" >    <TableRow>        <TextView            android:id="@+id/ItemText"            android:text="TItemTitle"            android:layout_width="320dp"            android:layout_height="36dp"            android:textSize="25dp"            android:layout_gravity="left"            android:gravity="left"            android:paddingLeft="10dp" />        <TextView            android:id="@+id/ItemTime"            android:text="ItemTime"            android:textSize="25dp"             android:layout_width="260dp"            android:layout_height="36dp"            android:layout_gravity="right"            android:gravity="left"            android:paddingRight="10dp"/>    </TableRow></TableLayout>

Add to listview when querying data

Variable declaration:

private ListView mlistview;private SimpleAdapter mlistItemAdapter;private ArrayList<HashMap<String, Object>> mlistItem = null;private DatabaseHelper dbHelper;

Add:

Mlistview = (listview) findviewbyid (R. id. mlistview); mlistitem = new arraylist 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.