发布时间:2019-07-12 10:02:51编辑:auto阅读(1259)
写得很辛苦,大家觉得好的话请多多回帖支持我,有时间会发布第二篇
演示环境
- /*class DBHelper code start*/
- package oms.sqlite_demo;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.util.Log;
- /**
- * 数据库连接类
- *
- * @author yifangyou 2010-01-15
- *
- */
- public class DBHelper extends SQLiteOpenHelper {
- private static SQLiteDatabase db = null;
- /**
- * 连接数据库,数据库不存在则创建
- *
- * @param Context context, 内容上限文
- * @param String dbname, 数据库名字
- * @param CursorFactory factory, 数据库游标
- * @param int version, 数据库版本
- * @return SQLiteDatabase 返回数据库操作类
- */
- public DBHelper(Context context, String dbname, CursorFactory factory,
- int version) {
- super(context, dbname, factory, version);
- }
- /**
- * 连接数据库,数据库不存在则创建
- *
- * @return SQLiteDatabase 返回数据库操作类
- */
- public SQLiteDatabase getconnect() {
- if (db == null) {
- db = this.getWritableDatabase();
- }
- return db;
- }
- /**
- * 断开数据库连接
- *
- * @return void
- */
- public void close() {
- if (db != null) {
- db.close();
- }
- }
- @Override
- public void onCreate(SQLiteDatabase arg0) {
- }
- @Override
- public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
- }
- }
- /* class DBHelper code end */
三、操作数据库
在sqlite_demo.java里的代码如下
- package oms.sqlite_demo;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.util.Log;
- public class sqlite_demo extends Activity {
- private final String DB_NAME = "testdb";
- private final String TABLE_NAME = "student";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- DBHelper dbh=new DBHelper(this.getBaseContext(),DB_NAME, null,1);
- SQLiteDatabase db= dbh.getconnect();
- createTable(db);
- queryTable(db);
- dbh.close();
- }
- public void createTable( SQLiteDatabase db){
- db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
- + "stud_no" + " TEXT,"
- + "stud_name" + " TEXT"
- + ");");
- String sql_1 = "insert into "+ TABLE_NAME +" (stud_no, stud_name) values('S777', 'Lily Chang');";
- String sql_2 = "insert into " + TABLE_NAME +" (stud_no, stud_name) values('S888', 'Linda Lin');";
- String sql_3 = "insert into " + TABLE_NAME +" (stud_no, stud_name) values('S999', 'Bruce Wang');";
- db.execSQL(sql_1); db.execSQL(sql_2); db.execSQL(sql_3);
- }
- public void queryTable(SQLiteDatabase db){
- String str="select * from "+TABLE_NAME;
- Cursor c =db.rawQuery(str,null);
- Log.v("loginactive","查询记录");
- Log.v("loginactive",String.valueOf(c.getCount()) );
- if(c!=null){
- c.moveToFirst();
- while(!c.isLast()){
- Log.v("loginactive","outp.");
- String stud_no=c.getString(0);
- String stud_name=c.getString(1);
- System.out.println(stud_no+"---------------"+stud_name);
- Log.v("loginactive",stud_no+"---------------"+stud_name);
- c.moveToNext();
- }
- }
- }
- }
六、查看结果
点击run后马上点击右上角的DDMS就能在log里看到结果
七、命令行下查看数据库
Cd $OPHONE_HOME/tools
adb pull /data/data/oms.sqlite_dmoe/databases/testdb test.db
sqlite3 test.db
上一篇: 车载3G无线共享方案
下一篇: 基于GNS3的独臂路由配置
47481
45782
36781
34305
28952
25587
24432
19603
19092
17624
5455°
6038°
5554°
5629°
6555°
5367°
5367°
5873°
5847°
7160°