Simple use of Android SQLite

Source: Internet
Author: User

Add, delete, modify, create, and modify table structures:
Void execsql (string SQL)

Query:
Cursor rawquery (string SQL, string [] selectionargs)

Write a simple example:
PackageCN. AA;

ImportAndroid. App. activity;

ImportAndroid. database. cursor;

ImportAndroid. database. SQLite. sqlitedatabase;

ImportAndroid. database. SQLite. sqliteexception;

ImportAndroid. OS. Bundle;

Public
ClassSimplesqlitetestactivityExtendsActivity {


/** Called when the activity is first created .*/


@ Override


Public voidOncreate (bundle savedinstancestate ){

Super. Oncreate (savedinstancestate );

Setcontentview (R. layout.Main);

// Just give a brief look. The interface is ignored.

Try{

// 1. Create or open a database

// The last parameter can be selected based on actual conditions.

// Sqlitedatabase. create_if_necessary,

// Sqlitedatabase. open_readonly,

// Or sqlitedatabase. create_if_necessary + sqlitedatabase. open_readonly

// Alternatively, you can use the method taught by Mars to create

String dbname = "/data/CN. AA/userinfo_db ";

Sqlitedatabase DB = sqlitedatabase.Opendatabase(Dbname,Null, Sqlitedatabase.Create_if_necessary);


// 2. Create a table

// The SQL statement that I want to execute to create a table in an opened database is:

// Create table t_userinfo (userid int primary key, name varchar (100), height double)

// The parameters of the execsql method can be written as a definite SQL statement string, or you can use a string to connect to more flexibly spell out the desired SQL statement

// Remember to comment out this sentence during the second execution. The same name cannot be created again.

Db.exe csql ("create table t_userinfo (userid int primary key, name varchar (100), height double )");


// 3. Add a record

// The SQL statement that I want to execute to add records to the t_userinfo table is:

// Insert into t_userinfo values (1, "Zhang San", 1.85)

// Note that the following SQL statements are spelled out by concatenating strings.

// Note that the double quotation marks in the SQL statement must be written as escape characters \"

IntUserid = 1;

Db.exe csql ("insert into t_userinfo values (" + userid ++ ", \" Zhang San \ ", 1.85 )");

Db.exe csql ("insert into t_userinfo values (" + userid ++ ", \" Li Si \ ", 1.76 )");

Db.exe csql ("insert into t_userinfo values (" + userid ++ ", \" \ ", 1.82 )");

// 4. Modify the record

// The SQL statement that I want to execute to modify records in the t_userinfo table is:

// Update t_userinfo set height = 1.65 where name = "zhangsan"

String username = "zhangsan ";

Db.exe csql ("Update t_userinfo set height = 1.65 where name = \" "+ username + "\"");


// 5. Delete the record

// The SQL statement that I want to execute to delete records in the t_userinfo table is:

// Delete from t_userinfo where name = "Li Si"

// Write the SQL statement to death this time. Don't fight, tired ......

Db.exe csql ("delete from t_userinfo where name = \" \"");


// 6. Query

// The SQL statement I want to query in the t_userinfo table is:

// Select * From t_userinfo

// The rawquery method is used. The returned value is a cursor.

// You can choose to splice your SQL statements or use wildcards? With the second selectionargs parameter to achieve flexibility, the effect is the same

Cursor rows = dB. rawquery ("select * From t_userinfo ",Null);

While(Rows. movetonext ()){

IntId = rows. getint (rows. getcolumnindex ("userid "));

String name = rows. getstring (rows. getcolumnindex ("name "));

DoubleHeight = rows. getdouble (rows. getcolumnindex ("height "));

System.Out. Println (ID + "\ t" + name + "\ t" + height );

}

 

// Do not forget to close the database.
DB. Close ();

}Catch(Sqliteexception e ){

E. printstacktrace ();

}

}

}

Well, it depends on whether you want to master SQL.
I think that SQL is the only choice, and there are very few tips (in total, there are creat, alter, insert, update, delete, and select statements, three fewer tricks than the only choice, in addition, if the changes are complex, select is a trick. The power is extremely high (it can cope with all database operations) and the changes are extremely complex (mainly select, which is called a constant change)

If you will not be able to use SQL in the future, consider the functions in the sqlitedatabase class and clarify the meaning of each parameter.

However, I think that since you have used SQLite, you will use other databases in the future. It is better to learn SQL well (It is unimaginable that the database does not support SQL ). In that case, the advantages of this method are obvious. As long as you can write SQL statements, these two functions are enough.

You only need to know three points:
1. SQL statements are written as strings as function parameters.
2. You can splice SQL statements for flexibility.
3. Note that the quotation marks in SQL statements are written as escape characters.

**************************************** ************************************

 

1. // obtain the data warehouse and its location. Note that the stringbuilder is used here to save memory and reduce memory consumption during the string operation (memory application and release will occur during the string operation, which is relatively memory-consuming ).

2. stringbuilder Ji connection text _ S = new stringbuilder (60 );

3. Ji connects to the text _ S. append (this. getclass (). getpackage (). getname ());

4. Ji connection text _ S. insert (0, "/data /");

5. Ji connection text _ S. append ("/database/userinfo_db ");

 

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.