Android SQLite database summary
sqlite
SQLite is an ultra lightweight embedded database with a size of only a few hundred KB. However, its syntax supports the standard SQL syntax and follows the acid transaction of the database. Therefore, it is easy for developers who have studied other databases to master its use.
SQL syntax will not be introduced. Let's look directly at the use in Android
Sqliteopenhelper - encapsulated database operation auxiliary class, which needs to be rewritten
override method
Oncreate: initialize the database, create tables, and add initial data
Onupgrade: database operations during database version upgrade, such as backup and database deletion
common method
Getreadabledatabase() gets the sqlitedatabase object and operates the database
Getwritabledatabase() gets sqlitedatabase object and operates the database
Difference: when the disk space is full or not writable, method 1 will obtain the read-only database object, while method 2 will report an error. Under normal circumstances, all the obtained database objects are read-write.
Creating an Android database requires creating objects
Create a database class object in the activity class
After creating the data object, you can manipulate the data. The difference between the two methods has been mentioned above
Sqlitedatabase provides us with many methods to manipulate data
Delete: (int) delete (string table, string whereclause, string [] whereargs)
Table: table name
Where clause: where conditional column name placeholder id =?
Whereargs: parameter value array
Add: (long) insert (string table, string nullcolumnhack, contentvalues)
Nullcolumnhack: null column
Contentvalues values: store the added data through key value pairs. Key is the column value and value is the value
The bottom layer of the insert method is by splicing strings. If the content values are empty, the spliced SQL statement will report an error if it cannot be executed. Therefore, give a column that can be empty. When the content values are empty, it can also be executed. If you are interested, you can take a look at the source code
Update: (int) update (string table, contentvalues, string [] whereargs)
The meaning of the parameter is the same as above
Query: (cursor) query (Boolean distinct, string table, string [] columns, string selection, string [] selectionargs, string groupby, string having, string orderby, string limit)
The return value is a cursor. For simple queries with many query parameters, it is easier to write an SQL statement
Boolean distinct de duplication
String table table name
String [] columns column to query
String selection query criteria
String [] selectionargs query parameter value
String groupby grouping
String having grouping condition
String orderby sort
String limit paging query limit
Close database: (void) close()
Execute an SQL statement: (void) execsql (string SQL) add, delete, modify and query
Query SQL: (cursor) rawquery (string SQL, string [] selectionargs)
affair
Paste code to execute SQL statements and encapsulated methods
query
If you can't use parameters, just leave them empty. Just traverse the data and use the while loop
Cursor c = db.query("student",null);// Query and get cursor
to update
delete
increase
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!