Use SQLite database in android

Source: Internet
Author: User

Use SQLite database in android

SQLite database with its lightweight, small size and other characteristics, so that it is widely used in development, in the previous blog I also introduced the use of SQLite database in Cocos2d-x, this blog is to introduce the use of SQLite database in Android, Android directly integrated SQLite database, very convenient to use, do not need to add external files to the Cocos2d-x that way

I will use the SQLite database to achieve the effect shown in the following figure. After opening the app, the page shown is displayed.

 

After you click the createDatabase button, a prompt message is displayed in logcat, indicating that the database is successfully created. When you click the createDAtabase button again, the prompt message is no longer printed, because the database will not be created after it is created

 

After the database is created, you can find a database folder under the app package, and find the created database file under the databases folder.

After exporting people. db, you can use SQLite Expert to view the content in the created database.

Click the Insert button. The information of 10 students is inserted into the database.

 

After you press the Delete button, the "Liu Xi" information will be deleted. You can see that there is no Liu Xi information in the database.

After you click the Update button, the student IDs of all students are reduced by 1.

Click the Select button to find the names of all students in the database and the C ++ scores corresponding to the names, and output the results in logcat.

Implementation Method:
1. Use Android Studio to create an Android project and modify the activity_main.xml file.

 

 
  
  
  
  
  
  
  
  
  
 
 

2. Create a MyOpenHelper. java file and add the following code to MyOpenHelper. java.

 

 

Package com. fyt. databasedemo; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; import android. util. log; // create an abstract class SQLiteOpenHelper implementation class MyOpenHelperpublic class MyOpenHelper extends SQLiteOpenHelper {/*** MyOpenHelper constructor * @ param context * @ param name of the database file * @ param factory cursor factory (result set) * @ param version: version number of the database (for upgrade) */public MyOpenHelper (Context context, String name, SQLiteDatabase. cursorFactory factory, int version) {super (context, name, factory, version);} // call this method when creating a database @ Override public void onCreate (SQLiteDatabase db) {Log. d ("MainActivity", "database created successfully"); // create a student table db.exe cSQL ("create table student (_ id integer primary key autoincrement, name char (10 ), age integer, no integer, cpp float, math float, english float) ");} // call this method during Database Upgrade @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log. d ("MainActivity", "Database Upgrade successful ");}}

3. modify the code in MainActivity. java

 

 

Package com. fyt. databasedemo; import android. app. activity; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. OS. bundle; import android. util. log; import android. view. view; public class MainActivity extends Activity {// used to create the Helper Object private MyOpenHelper oh; // used to create the database object private SQLiteDatabase db; @ Override protected void onCreate (Bundle savedInstanceState) {super. onC Reate (savedInstanceState); setContentView (R. layout. activity_main);} // create a database public void createDatabase (View view) {// create a Helper Object oh = new MyOpenHelper (this, "people. db ", null, 1); // create a database object db = oh. getWritableDatabase ();} // Add public void Insert (View view) to the database {// Add 10 student db.exe cSQL ("insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" Liu Xi ", 19,100 1, 60, 98, 75}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" Wang Rui ", 20,100 2, 63, 90, 96}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" 中" ", 19,100 3, 90, 73, 82}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" Wang Lei ", 21,100 4, 87, 86, 92}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" Feng song ", 19,100 5, 89, 98, 83}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" Pei ", 20,100 6, 75, 82, 91}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" {", 19,100 7, 62, 67, 90}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" {", 20,100 8, 98, 84, 87}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" ", 19,100 9, 57, 68, 96}); db.exe cSQL (" insert into student (name, age, no, cpp, math, english) values (?, ?, ?, ?, ?, ?) ", New Object [] {" He Qi ", 21,101 0, 61, 96, 72});} // Delete the public void Delete (View view) data in the database) {// delete the Student information db.exe cSQL ("delete from Student where name =? ", New Object [] {" "}) ;}// modify the public void Update (View view) data in the database) {// reduce the student ID of all users in the database by 1 db.exe cSQL ("update student set no = no-1");} // query the public void Select (View view) data in the database) {// query the name of a database student and the corresponding C ++ score. The returned value is a result set Cursor cursor = db. rawQuery ("select name, cpp from student", null); while (cursor. moveToNext () {// cursor. getColumnIndex ("name") obtains the name of the column String name = cursor. getString (cursor. getColumnIndex ("name"); float cpp = cursor. getFloat (cursor. getColumnIndex ("cpp"); // output the student's name and the C ++ score Log corresponding to the name. d ("MainActivity", '[' + name + "," + cpp + ']') ;}}

 

 

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.