How to realize Database programming in Android
This article mainly introduces the relevant knowledge of how to achieve database programming in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Android article on how to achieve database programming. Let's take a look at it.
The location of Android database storage is in data/data//databases/
1: create an Android database
Context.createDatabase (String name,int version, int mode,CursorFactory factory) throws a FileNotFoundException exception if the creation is not successful
Java code
SQLiteDatabase DataBase=this.openOrCreateDatabase ("DataBase.db", MODE_PRIVATE, new CursorFactory () {/ / create a new database with the name Database Pattern MODE_PRIVATE public Cursor newCursor (SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {return null }}); SQLiteDatabase DataBase=this.openOrCreateDatabase ("DataBase.db", MODE_PRIVATE, new CursorFactory () {/ / create a new database with the name Database, schema MODE_PRIVATE public Cursor newCursor (SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {return null;}})
2: open the existing database Context.openDatabase (String file,CursorFactory factory); if we want to open a database that does not exist, create it as follows:
Java code
/ / create a database named: DataBase with the suffix .db SQLiteDatabase DataBase=this.openOrCreateDatabase ("DateBase.db", MODE_PRIVATE, null) / / you can also write try {/ / Open the existing database db = this.openDatabase ("DateBase.db", null) } catch (FileNotFoundException e) {try {/ / create a new database} catch (FileNotFoundException e) {db = null }} / / create a database named: DataBase with the suffix .db SQLiteDatabase DataBase=this.openOrCreateDatabase ("DateBase.db", MODE_PRIVATE, null); / / you can also write try {/ / Open the existing database db = this.openDatabase ("DateBase.db", null) } catch (FileNotFoundException e) {try {/ / create a new database} catch (FileNotFoundException e) {db = null;}}
3: close the database
Java code
/ / Don't forget to close the database, oh DataBase.close (); / / never forget to close the database, oh, DataBase.close ()
4: delete the specified database
Java code
This.deleteDatabase ("Database.db"); this.deleteDatabase ("Database.db")
5: execute the SQL command using:
This is the end of the SQLiteDatabase.execSQL (String sql) article on "how to implement database programming in Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize database programming in Android". If you want to learn more knowledge, you are welcome to follow the industry information channel.