Android Technology--Database (c): an example of SQLite database application

Source: Internet
Author: User

The examples in this article are based on the addition of the items in the Android Review (eight): Network communications (i).

The effect is: in Postactivity, when the beauties are loaded, their information is written asynchronously to 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 parameters of the database
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 to create database
private static final String database_create = String.Format (
"CREATE table%s (%s integer primary key AutoIncrement,%s text not null,%s text is not NULL,%s text is not NULL,%s text not Null,%s float not null,%s float is not null,%s text is not null,%s float is not null,%s float is 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);
Defining constructors
Public Meinvdbhelper (context context, int version)
{
Sqliteopenhelper (context context, String name, Cursorfactory factory,
int version)
First parameter: Create the context of the database, for example, if an instance of Com.example.meinvliulanqi.ui.MainActivity is passed in, the actual storage location of the database file on the user's phone will be:/data/data/ Com.example.meinvliulanqi.ui/databases
The second parameter: the name of the database to be created, such as the name Meinvdb.db, after the successful creation of the database/data/data/com.example.meinvliulanqi.ui/ Databases path will have meinvdb.db, meinvdb.db-journal two files, one is the database file, the other is the log file
The third and fourth parameters: Cursorfactory is used to produce the cursor object, version is the build of the database
Super (context, db_name, NULL, version);
}
Override the OnCreate function to create a new database when the database does not exist on disk
@Override
public void OnCreate (Sqlitedatabase arg0)
{
Arg0.execsql (database_create);
}
Overriding the Onupgrade function, when there is a database version inconsistency, upgrade the database on disk to the current version, overwriting the installation may occur
@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.execute (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 < img_infos.length; 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.execute (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
}
}

The effect of the implementation:

When the page is loaded, ADB can see it under the/data/data/com.example.meinvliulanqi.ui/databases path:


Copy this meinvdb.db file and use the Sqlite3 tool to see:


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

Android Technology--Database (c): an example of SQLite database application

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.