The example of this article is about the implementation of database SQL programming for Android programming. Share to everyone for your reference. The specific analysis is as follows:
Install lightweight database SQLite in Android, and now test the database for basic operations.
Database basic operation: CREATE TABLE, INSERT, delete can use Execsql (), read can use Rawquery (), both functions can be standard SQL statement operation.
Source:
Package com.test.sql;
Import android.app.Activity;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.os.Bundle;
Import Android.util.Log;
public class Test_sql extends activity {databasehelper mopenhelper;
private static final String database_name = "TEST.DB";
private static final int database_version = 1; private static class Databasehelper extends Sqliteopenhelper {databasehelper (context) {Super (context, DAT
Abase_name, NULL, database_version); @Override public void OnCreate (Sqlitedatabase db) {//Create a new user table//Total 5 items: Id Pwd Name onlinetime level D
B.execsql ("CREATE TABLE user_table" (' Id ' INT, ' Pwd ' VARCHAR, ' Name ' VARCHAR, ' onlinetime ' int, ' level ' int) "); @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}/** called when the The activity is the created. */
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mopenhelper = new Databasehelper (this);
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
Clears the data db.execsql ("DELETE from user_table"); Insert data 10000 123456 "JDH" 0 0//Insert data 10001 123456 "Jim" 0 0 db.execsql ("INSERT INTO user_table VALUES (?,?,?,?,?)", NE
W object[]{10000, "123456", "Jdh", 0,0});
Db.execsql ("INSERT into user_table VALUES (?,?,?,?,?)", New object[]{10001, "123456", "Jim", 0,0});
Read data Cursor Cursor = Db.rawquery ("SELECT * from user_table WHERE Name =?", New string[]{"JDH"}); while (Cursor.movetonext ()) {String str = cursor.getint (0) + cursor.getstring (1) + cursor.getstring (2) + cursor.ge
TInt (3) + cursor.getint (4);
LOG.I ("str:", str); }
}
}
I hope this article will help you with your Android program.