First database databases are created
1. Create a database package, create a mydatabasehelper in the package to inherit the Databasehelper class, and implement the method inside!
3 ImportAndroid.content.Context;4 Importandroid.database.sqlite.SQLiteDatabase;5 ImportAndroid.database.sqlite.SQLiteOpenHelper;6 ImportAndroid.widget.Toast;7 8 /**9 * Database Helper classTen * Created by Administrator on 2017/12/6. One */ A - Public classMydatabasehelperextendssqliteopenhelper{ - the Public Static FinalString create_userinfo= "CREATE TABLE USERINFO ("//to create a table's SQL statement -+ "ID integer primary key autoincrement,"//ID Set Primary key -+ "username text,"//User name -+ "Age Integer,"//Age ++ "Sex text,"//Sex -+ "mautograph text)";//Signature + PrivateContext Mcontext; A at - /** - * Constructed method of generation - * @paramContext Get contexts - * @paramname - * @paramFactory in * @paramversion - */ to PublicMydatabasehelper (context context, String name, Sqlitedatabase.cursorfactory factory,intversion) { + Super(context, name, Factory, version); -Mcontext=context;//Get Context the } * $ Panax Notoginseng /** - * Implementation Method the * CREATE DATABASE + * @paramDB A */ the @Override + Public voidonCreate (Sqlitedatabase db) { -Db.execsql (Create_userinfo);//create a database with parameters as a statement of creation $Toast.maketext (Mcontext, "CREATE table succeeded", Toast.length_long). Show (); $ } - - /** the * Implementation Method - * Upgrade DatabaseWuyi * @paramSqlitedatabase the * @paramI - * @paramI1 Wu */ - @Override About Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intIintI1) { $ - } -}
2. Upgrade the database,
1 /**2 * Implementation Method3 * Upgrade Database4 * @paramSqlitedatabase5 * @paramI6 * @paramI17 */8 @Override9 Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intIintI1) {TenSqlitedatabase.execsql ("drop table if exists create_userinfo");//Deleting a database OneSqlitedatabase.execsql ("drop table if exists create_contact");//Deleting a database A } - - PrivateMydatabasehelper Mdbhelper;//declaring a database helper class variable the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +Mdbhelper=NewMydatabasehelper ( This, "This,userinfo",NULL, 3); //3 represents an upgrade - } - /** - * CREATE database at startup - */ - @Override in protected voidOnStart () { - Super. OnStart (); toMdbhelper.getwritabledatabase ();//get a writable database, create a helper database +}
3. Querying the Database
1 @Override2 PublicList<userinfo>GetUserInfo (Mydatabasehelper db) {3Sqlitedatabase sqlhelper=db.getwritabledatabase ();4Cursor cursor=sqlhelper.query ("USERINFO",NULL,NULL,NULL,NULL,NULL,NULL);5 UserInfo user;6List<userinfo> list=NewArraylist<>();7 if(Cursor.movetofirst ()) {8 Do{9String name=cursor.getstring (Cursor.getcolumnindex ("username"));TenString age=cursor.getstring (Cursor.getcolumnindex ("Age")); OneString sex=cursor.getstring (Cursor.getcolumnindex ("Sex")); AString phone=cursor.getstring (Cursor.getcolumnindex ("Phone")); -User=NewUserInfo (name,age,sex,phone); - list.add (user); the} while(Cursor.movetonext ()); - } - cursor.close (); - returnlist; +}
4. Inserting a database Mydatabasehelper databases inherits to Databasehelper
1 @Override2 Public voidsetuserinfo (mydatabasehelper database,userinfo user) {3Sqlitedatabase db=database.getwritabledatabase ();4Contentvalues values=Newcontentvalues ();5Values.put ("username", User.getname ());6Values.put ("Age", User.getage ());7Values.put ("Sex", User.getsex ());8Values.put ("Phone", User.getphone ());9Db.insert ("USERINFO",NULL, values);Ten}
5. Modify the Database
1 @Override2 Public voiddelectuserinfo (Mydatabasehelper database) {3Sqlitedatabase sqlitedatabase=database.getwritabledatabase ();4Contentvalues values=Newcontentvalues ();5Values.put ("Price", 10.99);//the field that needs to be modified Price field 10.99 Value6 7 //Modify the data? Position new string[]{modified? Data}8Sqlitedatabase.update ("USERINFO", Values, "Name=?",Newstring[]{"Thedavicicode"});9}
6. Delete data
1 @Override 2 Public void delectuserinfo (Mydatabasehelper database) {3 // delete data with ID less than 5 4 Sqlitedatabase db=database.getwritabledatabase (); 5 Db.delete ("USERINFO", "ID > 5",new string[]{"5"}); 6 }
SQLite use (Create, raise, increase, delete, change, check)