First, the creation and operation of database in Android
In Android, if you do a lot of storage with the same data structure, you need to use the SQLite database.
features of 1.SQLite:
1) It is a lightweight database that is actually a file. Of course it has a limited capacity, after all it is running in
The Android system.
2) Although it is small, it is very powerful and easy to operate.
creation of 2.SQLite
1th Step: Create a class that inherits the Sqliteopenhelper class, implementing its 2 methods OnCreate () and Onupgrade ().
public class myopenhelper extends sqliteopenhelper {//originally constructed method should have 4 parameters, super call when the parameters are written dead, Only the context parameter is passed here. Public myopenhelper (Context context) {/* * Parameterscontext context name The SQLite database name factory is used to create the cursor, which generally writes NULL. version database version number */super (context, "user.db", null, 3);//Call the constructor of the parent class to initialize the database} /* The * oncreate database is called   when it is first created, and is not called if the database already exists. * sqlitedatabase database objects, you can execute SQL statements. */@Overridepublic void oncreate (sqlitedatabase db) {//This method is typically used to write code that creates a table Db.execsql ("create table info (_id integer primary key autoincrement,name varchar (20));//Note that the SQL statement here is the difference between SQL statements used in MySQL, and int becomes integer,autoincr There is no underscore in the middle of the ement}/* * onupgrade when the database is upgraded. * oldversion new version * newversion old version * Note cannot be added when modifying table structurerepeating field */@Overridepublic void onupgrade (sqlitedatabase db, int oldversion, Int newversion) {//This method is typically used to write code that modifies the table structure Db.execsql ("alter table info add phone varchar (11) ");}}
2nd step: Create an instance of the open helper class above which defines this database
Myopenhelper myopenhelper = new Myopenhelper (this);
3rd Step: Use the database to open the helper class to create a database (two ways)
The Getreadabledatabase () method first calls the Getwritabledatabase () method, which opens the database as read-only if the database is full on disk. Sqlitedatabase readabledatabase = Myopenhelper.getreadabledatabase (); It is recommended to use//getwritabledatabase () to open the database in read-write mode, and if the storage space on the disk is full, the error will be directly. Sqlitedatabase writabledatabase = Myopenhelper.getwritabledatabase ();
After the database is created successfully, it is generated in the/data/data/application package name/databases directory, which is the private directory. I also learned files and Shared_prefs directories before.
3.SQLite additions and deletions to check operation
The process of adding and deleting SQLite is consistent, except that the SQL statements executed in the middle are different.
"Returns a database object using the Database Assistant Open Assistant Sqlitedatabase
2 ways to execute SQL statements by Sqlitedatabase objects.
"Closes the database.
Here is the 2nd step above, execute SQL.
* Traditional way:
Sqlitedatabase.execsql ("SQL statement" [, dot-character parameter array object type]);
The disadvantage of this method is that it is easy to write the SQL statement wrong, such as Chinese characters, rounded characters and so on, and Execsql method
There is no return value to determine if the SQL operation was successful.
* Call method
The Sqlitedatabase class defines the method that corresponds to the SQL statement, although this method reduces the write SQL statement
The probability of wrong, but the parameters of the method is more, it is more troublesome to use.
public class userdaoimpl implements userdao{private myopenhelper sqlhelper;// Create the Sqliteopenhelper database in the constructor Open helper Object Public userdaoimpl (Context context) {sqlhelper = new Myopenhelper (context);} The 3rd parameter of the/** insert method for adding operations is the contentvalues type, with the underlying map. Access is the name and value of the field you want to add * . */@Overridepublic long add (userbean user) {//get the database object sqlitedatabase database = sqlhelper.getwritabledatabase ();//database.execsql ("Insert into info values (null,?,?)", New object[]{user.name,user.phone});//Use the sqlitedatabase to replace the SQL statement with a return-value method. Contentvalues values = new contentvalues (); Values.put ("name", user.name); Values.put (" Phone ", user.phone); Long row_id = database.insert (" info ", null, values); Database.close (); return row_id;} Delete Operation @overridepublic Long del (String name) {sqlitedatabase database = sqlhelper.getwritabledatabase ( );//database.execsql ("DELETE&NBSP;FROM&NBSP;INFO&NBSP;WHERE&NBSP;NAME&NBSP;LIKE&NBSP;?", New Object[]{name }); Long delete_rows = database.delete ("Info", "NAME&NBSP;LIKE&NBSP;?", new String[]{name}); //Note that the condition is database.close () without the where keyword; return delete_rows;} Update operation the 2nd parameter of the/** update method is the contentvalues type, with the underlying map. The key is the field to be changed * , the value is the corresponding value of the Modified field. * * @Overridepublic int update (userbean user) {SQLiteDatabase database = Sqlhelper.getwritabledatabase ();//database.execsql ("Update info set name = ?,phone = ? ",new object[]{user.name + " _ Modify ",user.phone + " _ Modify "}); Contentvalues values = new contentvalues (); values. put ("Phone", user.phone + "_ Modify"); Int update_rows = database.update ("Info", values, "Name=", new string[]{user.name});d atabase.close (); return update_rows;} Query operation the 2nd parameter of the/**query method is the item to query */@Overridepublic list<userbean> query () {SQLiteDatabase Database = sqlhelper.getwritabledatabase ();//Create a list collection and return the data list<userbean> list_user = new ArrayList<UserBean> ();//Get Query collection//cursor cursor = database.rawquery (" Select name,phone from info ", null); Cursor cursor = database.query ("info", new string[]{"name", "Phone"}, null, Null, null, null, null); if (Cursor != null & cursor.getcount () > 0) {while (Cursor.movetonext ()) {//Note that the index is relative to the cursor, not relative to the database, and the 1th column is namestring name = Cursor.getstring (0);//Get Phone string phone = cursor.getstring (1); List_user.add (New userbean (Name,phone));} Return list_user;} Return null;}}
4.SQLite Database of things
Transactions: Either multiple SQL statements are executed successfully at the same time, or both failed bank transfers//Open things db.begintransaction (); try {//Transfer Db.execsql ("Update account set money= money-200 where Name=?", New string[]{"John Doe"}); Db.execsql ("Update account set money= money+200 where Name=?", New string[]{"Zhang San"}); Set up the thing has been successfully executed db.settransactionsuccessful (); } finally {//end thing db.endtransaction (); }
5. Commands to execute the SQLite database under the Linux command line
This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1675481
Android Day02-sqlite Database operation and ListView detailed