This article will introduce
How to add an expected sqllite database, and perform operations such as query in the database
Previous Article introduced how to add data in SD card
In this case, we will only explain how to add new resources and directly explain the actions of the newly added and queried resources.
Public class db_sample {public static final string str_table_name = "table_name"; // table name ‑ public static final string str_field_id = "field_id "; // The specified bit name must public static final string str_field_name = "field_name"; // The specified bit name must Private Static Context context; Public db_sample (context) {This. context = context; // when the new class is used, it must be written into the context, because openorcreatedatabase is created by the method under the context} // The method pub of the table LIC static final void fncreatedb () {// mode_world_writeable, only permission for sqlitedatabase dbwrite = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_writeable, null); // create table string SQL = string. format ("create table % s (% s integer primary key autoincrement, % s text)", str_table_name, str_field_id, str_field_name); dbwrite.exe csql (SQL );} // method public static final long F Ninsert (string strtext) {sqlitedatabase dbwrite = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_writeable, null); contentvalues CV = new contentvalues (); cv. put (str_field_name, strtext); Return dbwrite. insert (str_table_name, null, CV); // if return-1 indicates that there is already exist, otherwise, the response will be returned in the response column} // method public static final int fnread (string strtext) {// mode_world_writeable, only sqlitedat permission can be obtained from the token. Abase dbread = context. openorcreatedatabase ("/sdcard/hi_test.db", context. mode_world_readable, null); cursor = dbread. rawquery ("select" + str_field_id + "from" + str_table_name + "where" + str_field_name + "=? ", New string [] {strtext}); If (cursor. movetofirst () {return cursor. getint (0) ;}return-1 ;}}
The call method is as follows:
public class test extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); db_sample db = new db_sample(this); db.fnCreateDB(); db.fnInsert("HI"); int iID = db.fnRead("HI"); }}