Basic Android SQLite operations

Source: Internet
Author: User

In my previous blog "Introduction to andorid SQLite", I gave a brief introduction to the role and features of SQLite. This blog will introduce you to its basic operations.

The sqlitedatabase class also provides functions to operate databases, including insert, delete, update, and query (). However, these functions require many parameters, it is suitable for beginners who do not know much about SQL statements. If you want to improve your skills, it is best to use exesql () and rawquery (). These two methods are straightforward and easy to understand.

InProgramDuring initialization, you must first create a database to update the database. Therefore, you must inherit a sqliteopenhelper abstract class. Here there are two methods: oncreate () and onupgreade, the former is used to create a database and initialize the database. The latter is used when the database version is updated.


In the following example, a database named mydb with version 1 is created and a user table is created in the database. The constructor is used to input the Database Name and version name constant. The parameter is the context object.

Public class datebasehelper extends sqliteopenhelper {public final static string name = "mydb"; public final static int version = 1; Public datebasehelper (context) {super (context, name, null, version) ;}@ overridepublic void oncreate (sqlitedatabase dB) {db.exe csql ("create table user (userid integer primary key autoincrement, username varchar (20), PWD varchar (12 )) ") ;}@ overridepublic void onupgrade (Sqlitedatabase dB, int oldversion, int newversion) {db.exe csql ("Drop table if exists user"); oncreate (db) ;}} use sqlitedatabase to operate SQLite databases. Public class datebaseservice {private datebasehelper dbhelper; Public datebaseservice (context) {This. dbhelper = new datebasehelper (context);} // save data. Public void save (User user) {sqlitedatabase DB = dbhelper. getwritabledatabase (); db.exe csql ("insert into user (username, PWD) values (?,?) ", New object [] {user. getname (), user. getpwd ()});} // update data public void Update (User user) {sqlitedatabase DB = dbhelper. getreadabledatabase (); db.exe csql ("update user set username = ?, Pwd =? Where userid =? ", New object [] {user. getUserName (), user. getpwd (), user. getuserid ()});} // search for data public user find (integer userid) {sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. rawquery ("select * from user where userid =? ", New string [] {userid. tostring ()}); While (cursor. movetonext () {string name = cursor. getstring (cursor. getcolumnindex ("username"); int Pwd = cursor. getint (cursor. getcolumnindex ("PWD"); int id = cursor. getint (cursor. getcolumnindex ("userid"); return new user (ID, name, PWD);} return NULL;} // Delete public void Delete (integer userid) {sqlitedatabase DB = dbhelper. getreadabledatabase (); db.exe csql ("delete from User where userid =? ", New object [] {userid});} public long getcount () {sqlitedatabase DB = dbhelper. getreadabledatabase (); cursor = dB. rawquery ("select count (*) from user", null); cursor. movetofirst (); Return cursor. getlong (0 );}

of course, The SQLite operation is more than this. Here is just some simple addition, deletion, and modification. .

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.