Android technology-database (III): SQLite database application instance, androidsqlite

Source: Internet
Author: User

Android technology-database (III): SQLite database application instance, androidsqlite

For the source code of this article, see: https://github.com/YongYuIT/MeiNvLiuLanQi


The example in this article is based on the project added in "Android system review (8): Network Communication (1.

The effect is: In PostActivity, when the beauty is loaded, their information is asynchronously written into the database.

/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/IDBOperate. java file:

Package com. example. meinvliulanqi. basic_service;
Import android. database. sqlite. SQLiteDatabase;
Public interface IDBOperate
{
Public boolean doOperate (SQLiteDatabase db );
Public void onDBOpreaterFinished (Boolean result );
}

/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/MeinvDbHelper. java file:

Package com. example. meinvliulanqi. basic_service;
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Public class MeinvDbHelper extends SQLiteOpenHelper
{
// Basic database Parameters
Public static final String DB_NAME = "meinvDB. db ";
//--------------------------------------------------------------
// Basic description of the data table, table name and column name in the table
Public static final String DB_TABLE_MEINV_INFO = "tab_meinv_info ";
Public static final String KEY_ID = "id ";
Public static final String KEY_HEAD_PATH = "head_path ";
Public static final String KEY_NAME = "name ";
Public static final String KEY_BIRTHDAY = "birthday ";
Public static final String KEY_ADDRESS = "address ";
Public static final String KEY_STATURE = "stature ";
Public static final String KEY_WEIGHT = "weight ";
Public static final String KEY_BUST = "bust ";
Public static final String KEY_WAISTLINE = "waistline ";
Public static final String KEY_HIP = "hip ";
//--------------------------------------------------------------
// SQL statement used to create a database
Private static final String DATABASE_CREATE = String. format (
"Create table % s (% s integer primary key autoincrement, % s text not null, % s float not null, % s float not null, % s text not null, % s float not null, % s float not null )",
DB_TABLE_MEINV_INFO,
KEY_ID,
KEY_HEAD_PATH,
KEY_NAME,
KEY_BIRTHDAY,
KEY_ADDRESS,
KEY_STATURE,
KEY_WEIGHT,
KEY_BUST,
KEY_WAISTLINE,
KEY_HIP );
// Define the constructor
Public MeinvDbHelper (Context context, int version)
{
// SQLiteOpenHelper (Context context, String name, CursorFactory factory,
// Int version)
// The first parameter: Create the context of the database. For example, if com. example. meinvliulanqi. ui. the actual storage location of the database file in the user's mobile phone is/data/com. example. meinvliulanqi. ui/databases
// The second parameter: name of the database to be created. For example, the name is meinvDB. after the database is successfully created,/data/com. example. meinvliulanqi. meinvDB is available in the ui/databases path. db, meinvDB. db-journal files: database files and log files
// The third and fourth parameters: CursorFactory is used to generate the Cursor object. version is the version used to create the database.
Super (context, DB_NAME, null, version );
}
// Rewrite the onCreate function to create a new database when no database exists on the disk.
@ Override
Public void onCreate (SQLiteDatabase arg0)
{
Arg0.execSQL (DATABASE_CREATE );
}
// Rewrite the onUpgrade function. If the database version is inconsistent, upgrade the database on the disk to the current version. This may happen after overwriting and installation.
@ Override
Public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2)
{
Arg0.execSQL ("drop table if exist" + DB_TABLE_MEINV_INFO );
OnCreate (arg0 );
}
}

/MeiNvLiuLanQi/src/com/example/meinvliulanqi/basic_service/Task_operate_db.java file:

Package com. example. meinvliulanqi. basic_service;
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. OS. AsyncTask;
Import android. util. Log;
Public class Task_operate_db extends AsyncTask <Void, Integer, Boolean>
{
Private Context context;
Private int db_version;
Private IDBOperate operate;
Public Task_operate_db (Context _ con, int _ ver, IDBOperate _ ope)
{
Context = _ con;
Db_version = _ ver;
Operate = _ ope;
}
@ Override
Protected Boolean doInBackground (Void... para)
{
MeinvDbHelper helper = new MeinvDbHelper (context, db_version );
SQLiteDatabase db = null;
Try
{
Db = helper. getWritableDatabase ();
Return operate. doOperate (db );
} Catch (Exception e)
{
Log. e ("thinking -------", e. getMessage ());
Return false;
}
}
@ Override
Protected void onProgressUpdate (Integer... values)
{
}
@ Override
Protected void onPostExecute (Boolean result)
{
This. operate. onDBOpreaterFinished (result );
}
}

/MeiNvLiuLanQi/src/com/example/meinvliulanqi/ui/PostActivity. java file:

Package com. example. meinvliulanqi. ui;
Import org. json. JSONArray;
Import org. json. JSONException;
Import com. example. meinvliulanqi. basic_service.IDBOperate;
Import com. example. meinvliulanqi. basic_service.IGetdata;
Import com. example. meinvliulanqi. basic_service.MeinvDbHelper;
Import com. example. meinvliulanqi. basic_service.Task_getimgs;
Import com. example. meinvliulanqi. basic_service.Task_operate_db;
Import com. example. meinvliulanqi. basic_service.Task_postdata;
Import android. app. Activity;
Import android. content. ContentValues;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteDatabase;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. GridView;
Public class PostActivity extends Activity implements IGetdata, IDBOperate
{
Private MeinvBaseAdapter ada;
Private GridView gid_meinv;
Private JSONArray meinvs;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_post );
Task_postdata task = new Task_postdata (this );
Task.exe cute (new String [] {
"Http: // 192.168.10.111: 8011/MeinvInfo. ashx ",
"{\" Method \ ": \" get_meinv \ ", \" check_num \ ": 1001 }"});
}
@ Override
Public void onGetInfoData (String info)
{
Try
{
Meinvs = new JSONArray (info );
InitUIInfo ();
} Catch (JSONException e)
{
Log. e ("thinking -------", e. getMessage ());
}
}
Private void initUIInfo ()
{
Gid_meinv = (GridView) findViewById (R. id. gid_meinv_post );
Ada = new MeinvBaseAdapter (meinvs, new Bitmap [meinvs. length ()], this );
Gid_meinv.setAdapter (ada );
String [] [] img_infos = new String [meinvs. length ()] [2];
For (int I = 0; I {
Try
{
Img_infos [I] [0] = meinvs. getJSONObject (I)
. GetString ("photoPath ");
} Catch (JSONException e)
{
Img_infos [I] [0] = "";
Log. e ("thinking -------", e. getMessage ());
}
Img_infos [I] [1] = I + "";
}
Task_getimgs task = new Task_getimgs (this );
Task.exe cute (img_infos );
Task_operate_db task_db = new Task_operate_db (this, 1, this );
Task_db.execute ();
}
@ Override
Public void onGetImgData (Bitmap [] img)
{
Ada. setImgs (img );
}
@ Override
Public boolean doOperate (SQLiteDatabase db)
{
Try
{
ContentValues newValues = new ContentValues ();
For (int I = 0; I <meinvs. length (); I ++)
{
NewValues. put (MeinvDbHelper. KEY_NAME, meinvs. getJSONObject (I)
. GetString ("name "));
NewValues. put (MeinvDbHelper. KEY_ADDRESS, meinvs
. GetJSONObject (I). getString ("address "));
NewValues. put (MeinvDbHelper. KEY_HEAD_PATH,
"/Sdcard/meinvliulanqi/" + I + ". jpg ");
NewValues. put (MeinvDbHelper. KEY_BUST, meinvs. getJSONObject (I)
. GetString ("bust "));
NewValues. put (MeinvDbHelper. KEY_HIP, meinvs. getJSONObject (I)
. GetDouble ("hip "));
NewValues. put (MeinvDbHelper. KEY_BIRTHDAY,
Meinvs. getJSONObject (I). getString ("birthday "));
NewValues. put (MeinvDbHelper. KEY_STATURE, meinvs
. GetJSONObject (I). getDouble ("stature "));
NewValues. put (MeinvDbHelper. KEY_WEIGHT, meinvs. getJSONObject (I)
. GetDouble ("weight "));
NewValues. put (MeinvDbHelper. KEY_WAISTLINE, meinvs
. GetJSONObject (I). getDouble ("waistline "));
Db. insert (MeinvDbHelper. DB_TABLE_MEINV_INFO, null, newValues );
}
Return true;
} Catch (Exception e)
{
Log. e ("thinking -------", e. getMessage ());
Return false;
}
}
@ Override
Public void onDBOpreaterFinished (Boolean result)
{
// TODO Auto-generated method stub
}
}

Implementation results:

After the page is loaded, adb can be viewed in the/data/com. example. meinvliulanqi. ui/databases path:


Copy the meinvDB. db file and use the Sqlite3 tool to view the file:


The result indicates that the data has been written to the database.

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.