SQLite is a lightweight database, integrated in Android, below to share their own learning.
There are some good instructions to use when accessing the information:
The basic curd statement
The following SQL statement gets 5 records, skipping the previous 3 recordsSelect* fromAccount limit5Offset3OrSelect* fromAccount limit3,5Insert statement:Insert intoTable name (field list)Values(Value list). Such as:Insert intoPerson (name, age)Values(' ATM ',3) UPDATE statement:UpdateTable nameSetField name = valuewhereThe conditional clause. Such as:UpdatePersonSetName= ' ATM 'whereId=1Delete statement:Delete fromTable namewhereThe conditional clause. Such as:Delete fromPersonwhereId=1
The difference between getwritabledatabase () and Getreadabledatabase ()
getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。但getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。注意:getWritableDatabase(),getReadableDatabase的区别是当数据库写满时,调用前者会报错,调用后者不会,所以如果不是更新数据库的话,最好调用后者来获得数据库连接。
A simple description of the cursor
CursorgetCount()moveToFirst()moveToNext()isAfterLast()getColumnNames()getColumnIndex()getString(),getInt()requery()close() 方法释放游标资源;
The specific code:
First: How to create a database
Create databases and tables.
The default save path for the database is: "data/data/com.example.sqlitetest/databases/"
Com.example.SqliteTest is his own project.
Public class dbopenhelp extends sqliteopenhelper { Public Dbopenhelp(Context context) {Super(Context,"User_atm.db",NULL,1);//TODO auto-generated constructor stub}@Override Public void onCreate(Sqlitedatabase db) {//TODO auto-generated method stubStringBuilder strSQL =NewStringBuilder (); Strsql.append ("CREATE TABLE IF not EXISTS user_info ("); Strsql.append ("user_id INTEGER PRIMARY KEY autoincrement not NULL,"); Strsql.append ("User_age INT not NULL,"); Strsql.append ("user_name VARCHAR () not NULL)"); Db.execsql (Strsql.tostring ()); }@Override Public void Onupgrade(Sqlitedatabase DB,intOldversion,intNewVersion) {//Database modification is done here, such as adding table fields}
Here is the code for adding and deleting changes and transactions:
Public classservicetest {///delete and change //Get the databaseDbopenhelp Dbopenhelp; Public servicetest(Context context) {super (); This. Dbopenhelp =NewDbopenhelp (context); }//Start transaction Public void transactiontest() {Sqlitedatabase db = Dbopenhelp.getwritabledatabase (); Db.begintransaction ();Try{Db.execsql ("Update user_info set user_age=user_age+1 where user_id=10"); Db.execsql ("Update user_info set user_age=user_age-1 where user_id=11"); Db.settransactionsuccessful (); }finally{db.endtransaction ();} }//Increase Public void Save(UserInfo UserInfo) {Sqlitedatabase db = Dbopenhelp.getwritabledatabase (); Db.execsql ("INSERT into User_info (user_id, user_age, user_name) VALUES (?,?,?)",NewOBJECT[]{USERINFO.GETUSER_ID (), Userinfo.getuser_age (), Userinfo.getuser_name ()}); }//Delete Public void Delete(Integer user_id) {Sqlitedatabase db = Dbopenhelp.getwritabledatabase (); Db.execsql ("Delete from User_info where user_id=?",NewOBJECT[]{USER_ID}); }//Modify Public void Update(UserInfo UserInfo) {Sqlitedatabase db = Dbopenhelp.getwritabledatabase (); Db.execsql ("Update user_info set user_age=?,user_name=?" where user_id=? ",NewObject[]{userinfo.getuser_age (), Userinfo.getuser_name (), userinfo.getuser_id ()}); }//Enquiry PublicUserInfoFind(Integer ID) {Sqlitedatabase db = Dbopenhelp.getreadabledatabase (); cursor cursor = Db.rawquery ("SELECT * from User_info where user_id=?",NewString[]{id.tostring ()});if(Cursor.movetofirst ()) {intuser_id = Cursor.getint (Cursor.getcolumnindex ("user_id"));intUser_age = Cursor.getint (Cursor.getcolumnindex ("User_age")); String user_name = cursor.getstring (Cursor.getcolumnindex ("user_name"));return NewUserInfo (user_id, user_name, user_age); } cursor.close ();return NULL; }}
Visual Database management tools –sqlite Expert Professional
You can download it on the Internet.
After downloading, open the software and enter the main interface:
The middle Red box section is for importing and deleting databases.
In the new table.
There are a number of features that can be re-imported to the device (the path that starts everywhere) when you're done modifying it.
view the database with cmd
Open the database under the development path
Simple database operation, query a table.
Also in the study, such as database monitoring, database triggers and so on. will be updated further.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SQLite Learning (continue to update)