Original link: http://www.orlion.ga/594/
One, add data
Sqliteopenhelper's Getreadabledatabase () or Getwritabledatabase () methods are available for creating and upgrading databases, and both methods return a Sqlitedatabase object , this object enables CRUD operations on the data.
An insert () method is provided in Sqlitedatabase, which receives three parameters, the first is the table name, and the second parameter is used to automatically assign NULL to some nullable columns without specifying the addition of data, which is generally not possible with this function, and is passed directly to null. The third parameter is a Contentvalues object that provides a series of put () method overloads to add data to the contentvalues by simply passing in each column name in the table and the corresponding data to be added.
First modify Activity_main.xml to add a button, and then modify the Mainactivity.java:
@Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate), Setcontentview (r.layout.activity_main);d Bhelper = new mydatabasehelper (this , "bookstore.db" , null , 2); button button = (Button) findviewbyid (r.id.create_database); Button.setonclicklistener (new view.onclicklistener () {@Overridepublic void onclick (view v) { Dbhelper.getwritabledatabase ();}}); button adddata = (Button) findviewbyid (r.id.add_data); Adddata.setonclicklistener (new View.onclicklistener () {@Overridepublic void onclick (view v) {sqlitedatabase db = dbhelper.getwritabledatabase (); Contentvalues values = new contentvalues (); Values.put ("Name", "book"); Values.put (" Author " , " Orlion "); Values.put (" pages ", 9); Values.put (" Price ", &Nbsp;15.1);d B.insert ("book", null, values); Values.clear (); Values.put ("Name", "Book2"); values.put ("Author", "Orlion"), Values.put ("pages", 9), Values.put ("Price" , 15.1);d B.insert ("book", null, values);}});}
In the Click event of the Add Data button, we first get the Sqlitedatabase object and then use Contentvalues to assemble the data to be added. If you're more careful, you should find that only four of the data in the Book table are assembled, and the ID column does not give it a value. This is because we set the ID column to self-increment when we created the table in front of it, and its value is automatically generated when it is in storage, so it is not necessary to assign a value to it manually. Next Call the Insert () method to add the data to the table, note that we actually add two data, the above code uses Contentvalues to assemble two different content, and calls two times the insert () method.
Second, update the data
Sqlitedatabase also provides a very useful update () method for updating the data, this method receives four parameters, the first parameter and the Insert () method, is also the table name, where you specify to update which table data. The second parameter is the Contentvalues object, which is to assemble the updated data here. The third and fourth parameters are used to constrain the updating of data in a row or rows, and the default is to update all rows if not specified.
To use in a project, first add a button to the Activity_main.xml:
<button android:id= "@+id/update_data" android:layout_width= "Match_parent" android:layout_height= "W Rap_content "android:text=" Update Data "/>
Then modify the Mainactivity.java:
@Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate), Setcontentview (r.layout.activity_main);d Bhelper = new mydatabasehelper (this , "bookstore.db" , null , 2); button button = (Button) findviewbyid (r.id.create_database); Button.setonclicklistener (new view.onclicklistener () {@Overridepublic void onclick (view v) { Dbhelper.getwritabledatabase ();}}); button adddata = (Button) findviewbyid (r.id.add_data); Adddata.setonclicklistener (new View.onclicklistener () {@Overridepublic void onclick (view v) {sqlitedatabase db = dbhelper.getwritabledatabase (); Contentvalues values = new contentvalues (); Values.put ("Name", "book"); Values.put (" Author " , " Orlion "); Values.put (" pages ", 9); Values.put (" Price ", &Nbsp;15.1);d B.insert ("book", null, values); Values.clear (); Values.put ("Name", "Book2"); values.put ("Author", "Orlion"), Values.put ("pages", 9), Values.put ("Price" , 15.1);d B.insert ("book", null, values);}); button updatedata = (Button) findviewbyid (r.id.update_data); Updatedata.setonclicklistener ( New view.onclicklistener () {@Overridepublic void onclick (view v) {sqlitedatabase db = dbhelper.getwritabledatabase (); Contentvalues values = new contentvalues (); Values.put ("Price", 19.0);d b.update (the "book" , values, "NAME&NBSP;=&NBSP;?", new string[] {"book"});});
This will change the price of "book" to 19.0 when you click on the button to update the data.
Third, delete data
Button DeleteData = (button) Findviewbyid (r.id.delete_data);d Eletedata.setonclicklistener (New View.onclicklistener ( {@Overridepublic void OnClick (View v) {sqlitedatabase db = Dbhelper.getwritabledatabase ();d b.delete ("book", "name =?", New string[] {"book"});});
Here we have added a delete button to delete Name=book's book.
Iv. Querying data
A query () method is also provided in sqlitedatabase for querying the data. The parameters of this method are very complex, and the shortest method overload also requires passing in seven parameters. Let's take a look at the meanings of these seven parameters, the first parameter needless to say, and of course the table name, indicating which table we want to look up
To consult the data. The second parameter is used to specify which columns to query, and if not specified, all columns are queried by default. The third and fourth parameters are used to constrain the query of data for a row or rows, and the default is to query the data for all rows without specifying it. The fifth parameter specifies the column that needs to go to group by, without specifying a group by operation for the query result. The sixth parameter is used to further filter the data after group by, without specifying that it is not filtered. The seventh parameter is used to specify how the query results are sorted, not specified to use the default sorting method, and the overloads of several other query () methods are actually similar.
When the query () method is called, a cursor object is returned, and all data that is queried is removed from the object.
Here we add a query button and then give it a binding event to fetch the data:
Button querydata = (Button) findviewbyid (r.id.query_data); Querydata.setonclicklistener (new View.onclicklistener () {@Overridepublic void onclick (view v) {sqlitedatabase db = dbhelper.getwritabledatabase (); Cursor cursor = db.query ("book", null, null, null, null, null, Null, null);if (Cursor.movetofirst ()) {do {string name = cursor.getstring ( Cursor.getcolumnindex ("name")); String author = cursor.getstring (Cursor.getcolumnindex ("author"));int pages = Cursor.getint (Cursor.getcolumnindex ("pages"));d ouble price = cursor.getdouble ( Cursor.getcolumnindex ("price")); LOG.D ("Databasedemo", "name = " + name); LOG.D ("Databasedemo", "author = " + author); LOG.D ("DatabasEdemo ", " pages = " + pages); LOG.D ("Databasedemo", "price = " + price);} while (Cursor.movetonext ());} Cursor.close ();}});
V. Using SQL to manipulate the database
Here's how to add data:
Db.execsql ("INSERT into book (name, author, pages, price) VALUES (?,?,?,?)", new string[] {"The Da Vinci Code", "Dan Bro WN "," 454 "," 16.96 "});d B.execsql (" INSERT into book (name, author, pages, price) VALUES (?,?,?,?) ", new string[] {" The L OST Symbol "," Dan Brown "," 510 "," 19.95 "});
Here's how to update your data:
Db.execsql ("update book Set price =?") WHERE name =? ", new string[] {" 10.99 "," The Da Vinci Code "});
Here's how to delete data:
Db.execsql ("Delete from book where pages >?", New string[] {"500"});
Here's how to query data:
Db.rawquery ("SELECT * from book", NULL);
Getting Started with Android (11) SQLite Curd