Previously thought SQLite can only create multiple tables at once, not actually
The key is that the operation of SQLite requires some skill:
PackageCom.example.mydemo;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.widget.Toast; Public classMydatabasehelperextendssqliteopenhelper{ Public Static FinalString create_book = "CREATE table book (" + "ID integer primary key autoincrement," + "author text , "+" price real, "+" pages integer, "+" Name text) "; Public Static FinalString create_category = "CREATE table CATEGORY (" + "ID integer primary key autoincrement," + "cat Egory_name text, "+" Category_code Integer) "; PrivateContext Mcontext; PublicMydatabasehelper (Context context, String name, Cursorfactory factory,intversion) { Super(context, name, Factory, version); Mcontext=context; } @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (create_book);//Db.execsql (create_category); /*Db.execsql ("INSERT into book (name, author, pages, price) VALUES (?,?,?,?)", new string[] {"The Da Vinci Code "," Dan Brown "," 454 "," 16.96 "});*/Toast.maketext (Mcontext,"Create succeeded", Toast.length_short). Show (); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO auto-generated Method Stub Switch(oldversion) { Case3: Db.execsql (create_category); Case4: Case5: Case6: Db.execsql ("ALTER TABLE book add column Category_idd integer"); default: } }}
At present very curious, how to get the database version (of course db.getversion ()) certainly can
But if it's just a. db file, you don't see which visualizer can be viewed directly
PackageCom.example.mydemo;Importandroid.app.Activity;Importandroid.app.Fragment;Importandroid.content.ContentValues;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;/*** A placeholder fragment containing a simple view.*/ Public classPlaceholderfragmentextendsFragment {Ondtareceivedlistener mcallback; PrivateMydatabasehelper DBHelper; //Container Activity must implement this interface Public InterfaceOndtareceivedlistener { Public voidontextselected (view view); } Publicplaceholderfragment () {} @Override Public voidOnattach (activity activity) {//TODO auto-generated Method Stub Super. Onattach (activity); Try{mcallback=(Ondtareceivedlistener) activity; } Catch(classcastexception e) {Throw Newclasscastexception (activity.tostring ()+ "must implement Ondtareceivedlistener"); }} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View R Ootview=inflater.inflate (R.layout.fragment_main, container,false); DBHelper=NewMydatabasehelper (Getactivity (), "bookstore.db",NULL, 7); Button CreateDatabase=(Button) rootview. Findviewbyid (R.id.create_database); Createdatabase.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {dbhelper.getwritabledatabase (); } }); Button AddData=(Button) Rootview.findviewbyid (r.id.add_data); Adddata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {sqlitedatabase db=dbhelper.getwritabledatabase (); Contentvalues Values=Newcontentvalues (); //start assembling the first piece of dataValues.put ("name", "The DaVinci Code"); Values.put ("Author", "Dan Brown."); Values.put ("Pages", 454); Values.put ("Price", 16.96); Db.insert ("Book",NULL, values);//Insert the first piece of datavalues.clear (); //start assembling the second piece of dataValues.put ("name", "The Lost Symbol"); Values.put ("Author", "Dan Brown."); Values.put ("Pages", 510); Values.put ("Price", 19.95); Db.insert ("Book",NULL, values);//Insert a second piece of datavalues.clear (); //start assembling the third piece of data to allow a single piece of data to be emptyValues.put ("name", "The Lost1"); Values.put ("Pages", 510); Values.put ("Price", 19.95); Db.insert ("Book",NULL, values);//Insert a second piece of data } }); Button UpdateData=(Button) Rootview.findviewbyid (r.id.update_data); Updatedata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {sqlitedatabase db=dbhelper.getwritabledatabase (); Contentvalues Values=Newcontentvalues (); Values.put ("Price", 10.99); Db.update ("Book", Values, "name =?", NewString[] {"The DaVinci Code" }); //The intent of the above code is to change the price of the book named the Da Vinci code to 10.99 } }); Button DeleteButton=(Button) Rootview.findviewbyid (r.id.delete_data); Deletebutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {sqlitedatabase db=dbhelper.getwritabledatabase (); Db.begintransaction (); //Open TransactionDb.delete ("book", "name =?",NewString[] {"The Da Vinci Code" }); Db.delete ("Book", "id =?",NewString[] {"4" }); } }); returnRootview; }}
Layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Button Android:id= "@+id/create_database"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Create database"/> <Button Android:id= "@+id/add_data"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "ADD data"/> <Button Android:id= "@+id/update_data"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Update data"/> <Button Android:id= "@+id/delete_data"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Delete data"/></linearlayout>
The use of "Android Advanced" SQLite