Write SQL Statement Operations Database (MU-class SQLite notes)

Source: Internet
Author: User
Tags sqlite
One of the commonly used Android data storage methods SQLite study and operation notes

0. Video address: http://www.imooc.com/video/3382

1. Each program has its own database. By default, they do not interfere with each other.

1) Create a database and open it;

SQLiteDatabase db = openOrCreateDatabase ("user.db", MODE_PRIVATE, null);
2) The use of cursor cursor is equivalent to a collection of stored results, which can be understood as a list;

3) The cursor must be released after the end.

2. Specific code:

 1 public class MainActivity extends Activity {
 2 
 3 @Override
 4 protected void onCreate (Bundle savedInstanceState) {
 5 super.onCreate (savedInstanceState);
 6 setContentView (R.layout.activity_main);
 7
 8 // Each program has its own database. By default, they do not interfere with each other.
 9 // 1. Create a database and open
10 SQLiteDatabase db = openOrCreateDatabase ("user.db", MODE_PRIVATE, null);
11 db.execSQL ("create table if not exists usertb (_id integer primary key autoincrement," +
12 "name text not null, age integer not null, sex text not null)");
13 // Create user table including _id primary key, name, age, gender
14 db.execSQL ("insert into usertb (name, age, sex) values (‘ Zhang San ’,‘ Male ’, 26)");
15 db.execSQL ("insert into usertb (name, age, sex) values (‘ Liu Ming ’,‘ Male ’, 22)");
16 db.execSQL ("insert into usertb (name, age, sex) values (‘ Yu Si ’,‘ Female ’, 21)");
17
18 // 2. The use of cursor cursor is equivalent to storing a collection of results, which can be understood as a list
19 Cursor c = db.rawQuery ("select * from usertb", null);
20 if (c! = Null) {
21 while (c.moveToNext ()) {
22 Log.i ("info", "_ id:" + c.getInt (c.getColumnIndex ("_ id")));
23 Log.i ("info", "name:" + c.getString (c.getColumnIndex ("name")));
24 Log.i ("info", "sex:" + c.getString (c.getColumnIndex ("sex")));
25 Log.i ("info", "age:" + c.getInt (c.getColumnIndex ("age")));
26 Log.i ("info", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
27}
28 c.close (); // 3. Release cursor
29}
30 db.close ();
31}
32}
View Code
3. Operation result:

4. Knowledge points:

1) You can use the tool navicat to open and view the db format file and view the user table created by yourself;

2) Cursor related:

 

Write SQL statements to operate the database (Mu Class SQLite notes)
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.