SQLite database with its light weight, small size and so on, so that its application in the development of a very wide, in the previous blog I also introduced in Cocos2d-x in the use of SQLite database, this blog is to introduce the use of the SQLite database in Android, The SQLite database is integrated directly into Android and is easy to use, without the need to add external files to cocos2d-x.
I will use the SQLite database to implement an example of the effect, open the app will pop up the interface shown
After clicking the CreateDatabase button, you can see in Logcat that a message was printed indicating that the database was created successfully and that the prompt will not be printed when the CreateDatabase button is clicked again, because the database is not created again after it is created successfully
After the database has been created successfully, you can find a database folder under the package under the app, and the Databases folder can find the file created.
After exporting people.db, you can view the content in the created database using SQLite expert
When you click the Insert button, you can see that you have inserted 10 student information into the database
Press the Delete button will delete the "Liu Li" message, you can see the database has no information about Liu's pride
When you click the Update button, you reduce all student numbers by 1
After clicking the Select button, look for the names of all students in the database and the C + + scores corresponding to their names and output in Logcat
Implementation method:
1. Create an Android project using Android Studio and modify the Activity_main.xml file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.fyt.databasedemo.MainActivity "android:orientation=" vertical "> <button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "createDatabase "Android:textsize=" 30DP "android:onclick=" CreateDatabase "/> <button android:layout_width=" m Atch_parent "android:layout_height=" wrap_content "android:text=" Insert "android:textsize=" 30DP " android:onclick= "Insert"/> <button Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Delete" Android:textsize= "30DP" android:onclick= "Delete"/> <button android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Update" android:textsize= "30DP" android:onclic k= "Update"/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Select" android:textsize= "30DP" android:onclick= "select"/></linearlayout>
2. Create a new Myopenhelper.java file and add the following code in the 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 construction method * @param Context Context * @param name of database file * @param factory cursor Factory (Result set) * version number of @param version database (for upgrade) */Public Myopenhelper (context context, String name, Sqlitedatabase.cursorfactory factory, int version) {Super ( Context, name, Factory, version); }//When creating the database, call this method @Override public void OnCreate (Sqlitedatabase db) {log.d ("mainactivity", "Database Creation succeeded"); Create a student table Db.execsql ("CREATE TABLE Student" (_id integer primary key autoincrement, name Char (ten), age integer, No integer, CPP float, math float, 中文版 float) "); }//Database Upgrade Call this method @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {LOG.D ("Mainactivity", "Successful Database Upgrade"); }}
3, modify the code in the 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 helper object private Myopenhelper Oh; Used to create a database object private Sqlitedatabase db; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); }//CREATE database public void CreateDatabase (view view) {//create helper Object oh = new Myopenhelper (This, "people.db", n ull, 1); Create database Object db = Oh.getwritabledatabase (); }//Add data to the database public void Insert (view view) {///Add 10 students to the student table Db.execsql ("Insert into student (name, age , no, CPP, math, English) VALUES (?,?,?,?,?,?) ", New object[]{" Liu Smug ", 19, 1001, 60, 98, 75}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?, ?,?,?) ", New object[]{" Duan ", 20, 1002, 63, 90, 96}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"He Yuzhong", 19, 1003 , 90, 73, 82}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"Wang Lei", 21, 1004, 87, 86, 92}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"Feng song", 19, 1005, 89, 98, 83}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"Ms. Penny Pei", 20, 1006, 75, 82, 91}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"Ma Xiao", 19, 1007, 62, 67, 90}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"Ruby", 20, 1008, 98, 84, 87}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) values(?,?,?,?,?,?) ", New object[]{" Zhou June L ", 19, 1009, 57, 68, 96}); Db.execsql ("INSERT into student (name, age, no, CPP, math, 中文版) VALUES (?,?,?,?,?,?)", New object[]{"He Hui", 21, 1010, 61, 96, 72}); }//delete data in database public void Delete (view view) {//delete the information of the student named "Liu Db.execsql" ("Delete from Student where Name =? ", New object[]{" Liu Smug "}); }//Modify data in database public void Update (view view) {//Reduce the number of everyone in the database by 1 db.execsql ("Update student Set no = no -1 "); }//query data in database public void Select (view view) {//query database The name of the secondary school student and its corresponding C + + score, the return value is a result set cursor cursor = Db.ra Wquery ("Select Name, CPP from student", null); while (Cursor.movetonext ()) {//cursor.getcolumnindex ("name") gets the column that contains the name String name = cursor.getstring (Cursor.getcolumnindex ("name")); Float cpp = cursor.getfloat (Cursor.getcolumnindex ("CPP")); Output the student's name and the C + + score corresponding to the name LOG.D ("mainactivity", ' [' + name + ', ' + cpp + '] '); } }}
Using the SQLite database in Android