using the Sqliteopenhelper class operations database is an official help class, the operational database to use to Sqlitedatabase, can be getreadabledatabase () or Getwritabledatabase (), both methods can open or create a database, except that the database is not writable when the disk space is full, and getreadabledatabase () can open the database successfully, but only the database can be read , and using Getwritabledatabase () throws an exception directly.
You must first create a class that inherits Sqliteopenhelper
ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classMydatabasehelperextendsSqliteopenhelper {//to create a table's SQL statement Public Static FinalString create_student_table =
"CREATE TABLE student (ID integer primary key autoincrement,age integer, name text, number text)"; /*** Construction Method *@paramContext Contexts *@paramname of the database *@paramFactory Customizing a cursor *@paramVersion Data revision number*/ PublicMydatabasehelper (context context, String name, Sqlitedatabase.cursorfactory factory,intversion) { Super(context, name, Factory, version); } @Override Public voidonCreate (Sqlitedatabase db) {//Create a databaseDb.execsql (create_student_table);
LOG.E ("Mydatabasehelper", "Data sheet created");
}
@Override
Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {
//This method is executed when the value passed in version in the constructor method is higher than the current database .
}
}
If you want to create a database and execute the code below, you can also use Mydbhelper.getreadabledatabase ()
New Mydatabasehelper (This, "school",null, 1); // Open the database if it exists, or create a Sqlitedatabase writdb = Mydbhelper.getwritabledatabase ();
To facilitate the operation of the data, create a student class
Public classStudent {Private intID; PrivateString number; PrivateString name; Private intAge ; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetNumber () {returnNumber ; } Public voidSetnumber (String number) { This. Number =Number ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } @Override PublicString toString () {return"Student {" + "id=" + ID + ", study number = ' + ' + ' + ' + ', name = '" + name + ' \ ' + ", age =" + Ages + '} '; }}View Code
Inserting data
PrivateMydatabasehelper Mydbhelper;Privatesqlitedatabase Writdb;mydbhelper=NewMydatabasehelper ( This, "School",NULL, 1); Writdb=mydbhelper.getwritabledatabase (); Student Insertstu=NewStudent (); Insertstu.setnumber ("1238409"); Insertstu.setname ("Xiao Ming"); Insertstu.setage (21st);//Get DataContentvalues CV =Newcontentvalues (); Cv.put ("Number", Insertstu.getnumber ()); Cv.put ("Name", Insertstu.getname ()); Cv.put ("Age", Insertstu.getage ());//inserting data into the databaseWritdb.insert ("Student",NULL, CV);/*** SQL statement mode * WRITDB.EXECSQL ("INSERT into student (Number,name,age) VALUES (?,?,?)", * New String[]{ins Ertstu.getnumber (), Insertstu.getname (), String.valueof (Insertstu.getage ())}); */
Writdb.close (); Mydbhelper.close ();
Querying data
PrivateMydatabasehelper Mydbhelper;Privatesqlitedatabase Writdb;mydbhelper=NewMydatabasehelper ( This, "School",NULL, 1); Writdb=mydbhelper.getwritabledatabase ();//create a collection to hold the queried dataList<student> students2 =NewArraylist<>();//parameter description: ① table name (use global constants, avoid write errors), ② the column names to find, ③ constraints, ④ constraints, ⑤ grouping, ⑥ grouping, ⑦ sortingcursor cursor = writdb.query ("Student",NULL,NULL,NULL,NULL,NULL,NULL);/*** SQL statement mode * CURSOR cursor = writdb.rawquery ("SELECT * from student", null);*/if(Cursor! =NULL) { while(Cursor.movetonext ()) {Student Stu=NewStudent (); //get data in a rowStu.setid (Cursor.getint (Cursor.getcolumnindex ("id")))); Stu.setnumber (cursor.getstring (Cursor.getcolumnindex ("Number"))); Stu.setname (cursor.getstring (Cursor.getcolumnindex ("Name"))); Stu.setage (Cursor.getint (Cursor.getcolumnindex ("Age"))); //Add to CollectionStudents2.add (STU); } //Remember to closecursor.close ();} Writdb.close (); Mydbhelper.close (); for(Student student2:students2) {LOG.E ("Query Results", student2.tostring ()); }
3 Data has been inserted
Update data
PrivateMydatabasehelper Mydbhelper;Privatesqlitedatabase Writdb;mydbhelper=NewMydatabasehelper ( This, "School",NULL, 1); Writdb=mydbhelper.getwritabledatabase (); Student Updatetstu=NewStudent (); Updatetstu.setnumber ("11111"); Updatetstu.setage (32);//get the data to updateContentvalues CV =Newcontentvalues (); Cv.put ("Number", Updatetstu.getnumber ()); Cv.put ("Age", Updatetstu.getage ());//parameter description: ① table name, ② updated data, ③ condition, ④ condition valueWritdb.update ("Student", CV, "ID >?",Newstring[]{"1"});/*** SQL statement mode * WRITDB.EXECSQL ("update student set number =?, age =?") Where ID >? ", * new String[]{updatetstu.getnumber (), String.valueof (Updatetstu.getage ())," 1 "}); */writdb.close (); Mydbhelper.close ();
The results after the update
Delete data
private Mydatabasehelper Mydbhelper; private sqlitedatabase writdb;mydbhelper = new Mydatabasehelper (this , "school", null , 1< Span style= "color: #000000"); Writdb = Mydbhelper.getwritabledatabase (); // parameter description: ① table name, ② condition, ③ condition value writdb.delete ("Student", "id =?", new string[]{"2" }); /** * SQL statement mode * WRITDB.EXECSQL ("Delete from student where id =?", New string[]{"2"}); */
writdb.close (); Mydbhelper.close ();
The result after deletion
Learn about the Android database SQLite notes