Hello, everyone! If you feel sqliteopenhelper don't understand, you can look at it.
OnCreate (Sqlitedatabase db): This method is executed when the database is first created, and initialization operations such as table creation are typically performed in the method.
Directly on the code:
Create a new Qqltext class
PackageCOM.EXAMPLE.SJK;ImportAndroid.content.Context;ImportAndroid.database.sqlite.SQLiteCursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log; Public classSQLTextextendssqliteopenhelper{Private Static FinalString db_name = "mydata.db";//Database name Private Static Final intVersion = 1;//Database Version
Calling the parent class constructor PublicSQLText (Context context) {Super(Context, Db_name,NULL, version); //context Note } @Override Public voidOnCreate (Sqlitedatabase db) {//The newly installed software starts hereString sql_message = "CREATE TABLE t_message (ID int primary key,name varchar (), age varchar (50))"; Db.execsql (Sql_message); LOG.I ("Hi", "CREATE Table succeeded"); String Sql_up1= "INSERT into t_message values (1, ' small white ', ' 18 ')"; Db.execsql (SQL_UP1); LOG.I ("Hi", "version 1.0 insert data succeeded"); } @Override//updates on the original software will start here without uninstalling the online update Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { }}
Public void OnCreate (Sqlitedatabase db) //I understand that: the newly installed software starts here, the others are not related;
This time our database has been created, and the table has been built, the data is inserted
Select Export:
The database has data, small white garbled, but still inserted data.
Add Point query function Mainactivity.java
Package Com.example.sjk;import Android.app.activity;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.widget.textview;public Class Mainactivity extends Activity {private SQLText st;//SQLText contextprivate TextView TV for this category; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV = (TextView) Findviewbyid (R.id.textview);//Find this TextView component St = new SQLText (mainactivity.this); String text = query ();//Call Queries This method Tv.settext ("ID \ T" + "name\t" + "age\n" +text);//display} public String query ( {//Query database for data string result = ""; Get database object Sqlitedatabase db = St.getreadabledatabase ();//Read-only: readabledatabase; Read and write: Writabledatabase//Querying data in database cursor cursor = Db.query ("T_message", NULL, NULL, NULL, NULL, NULL, NULL);//result set for (int i=0;i<cursor.getcount (); i++) { Cursor.movetonext (); result + = CURsor.getint (Cursor.getcolumnindex ("id")); result + = Cursor.getstring (Cursor.getcolumnindex ("name")); result + = Cursor.getstring (Cursor.getcolumnindex ("Age")); Cursor.close ();//close result set db.close ();//Close database object return result; }}
Results:
On the oncreate example of Sqliteopenhelper