Write SQL statements to operate databases (MOOC SQLite notes) and sqlsqlite

Source: Internet
Author: User

Write SQL statements to operate databases (MOOC SQLite notes) and sqlsqlite

SQLite learning and operation notes, one of the Common Data Storage Methods for Android

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

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

1) Create a database and open it;

SQLiteDatabase db=openOrCreateDatabase("user.db",MODE_PRIVATE,null);

2) Using cursor is equivalent to a set of stored results, which can be understood as 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, each program does not interfere with each other. 9 // 1. create a database and open 10 SQLiteDatabase db = openOrCreateDatabase ("user. db ", MODE_PRIVATE, null); 11 db.exe cSQL (" create table if not exists usertb (_ id integer primary key autoincremen T, "+ 12" name text not null, age integer not null, sex text not null) "); 13 // create a user table including _ id Primary Key, name, age, gender 14 db.exe cSQL ("insert into usertb (name, age, sex) values ('zhang san', 'mal', 26 )"); 15 db.exe cSQL ("insert into usertb (name, age, sex) values ('Liu ming', 'male', 22 )"); 16 db.exe cSQL ("insert into usertb (name, age, sex) values ('yu si', 'female ', 21)"); 17 18 // 2. the cursor Cursor is equivalent to a set of stored results. It can be understood as list19 cursor c = db. rawQuery ("select * from usertb", null); 20 I F (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. Running result:

4. Note:

1) You can use the navicat tool to open and view db format files and view User tables you have created;

2) cursor:

 

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.