写得很辛苦,大家觉得好的话请多多回帖支持我,有时间会发布第二篇.
演示环境
Windows xp
Eclipse 3.4.2
Ophone SDK 1.0

一、创建工程
在Eclipse里
File->new->Android project->输入工程名sqlite_demo等等如下图:->点击完成
我学习ophone里使用sqlite3-LMLPHP
二、建立数据库连接类
在src下oms.sqlite_demo下新建DBHelper类
写代码如下
  1. /*class DBHelper code start*/
  2. package oms.sqlite_demo;

  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  7. import android.util.Log;

  8. /**
  9.  * 数据库连接类
  10.  *
  11.  * @author yifangyou 2010-01-15
  12.  *
  13.  */
  14. public class DBHelper extends SQLiteOpenHelper {
  15.     private static SQLiteDatabase db = null;
  16.     /**
  17.      * 连接数据库,数据库不存在则创建
  18.      *
  19.      * @param Context context, 内容上限文
  20.      * @param String dbname, 数据库名字
  21.      * @param CursorFactory factory, 数据库游标
  22.      * @param int version, 数据库版本
  23.      * @return SQLiteDatabase 返回数据库操作类
  24.      */
  25.     public DBHelper(Context context, String dbname, CursorFactory factory,
  26.             int version) {
  27.         super(context, dbname, factory, version);
  28.     }
  29.     /**
  30.      * 连接数据库,数据库不存在则创建
  31.      *
  32.      * @return SQLiteDatabase 返回数据库操作类
  33.      */
  34.     public SQLiteDatabase getconnect() {
  35.         if (db == null) {
  36.             db = this.getWritableDatabase();
  37.         }
  38.         return db;
  39.     }
  40.     /**
  41.      * 断开数据库连接
  42.      *
  43.      * @return void
  44.      */
  45.     public void close() {
  46.         if (db != null) {
  47.             db.close();
  48.         }
  49.     }

  50.     @Override
  51.     public void onCreate(SQLiteDatabase arg0) {

  52.     }

  53.     @Override
  54.     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

  55.     }
  56. }
  57. /* class DBHelper code end */

三、操作数据库

sqlite_demo.java里的代码如下

  1. package oms.sqlite_demo;

  2. import android.app.Activity;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.util.Log;


  7. public class sqlite_demo extends Activity {
  8.     private final String DB_NAME = "testdb";
  9.     private final String TABLE_NAME = "student";
  10.     /** Called when the activity is first created. */
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.         DBHelper dbh=new DBHelper(this.getBaseContext(),DB_NAME, null,1);
  16.         SQLiteDatabase db= dbh.getconnect();
  17.         createTable(db);
  18.         queryTable(db);
  19.         dbh.close();
  20.     }
  21.     
  22.     
  23. public void createTable( SQLiteDatabase db){
  24.         db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
  25.                 + "stud_no" + " TEXT,"
  26.                 + "stud_name" + " TEXT"
  27.                 + ");");
  28.     
  29.         String sql_1 = "insert into "+ TABLE_NAME +" (stud_no, stud_name) values('S777', 'Lily Chang');";
  30.         String sql_2 = "insert into " + TABLE_NAME +" (stud_no, stud_name) values('S888', 'Linda Lin');";
  31.         String sql_3 = "insert into " + TABLE_NAME +" (stud_no, stud_name) values('S999', 'Bruce Wang');";
  32.         db.execSQL(sql_1); db.execSQL(sql_2); db.execSQL(sql_3);
  33.     }
  34.     public void queryTable(SQLiteDatabase db){
  35.         String str="select * from "+TABLE_NAME;
  36.         Cursor c =db.rawQuery(str,null);
  37.         
  38.         Log.v("loginactive","查询记录");
  39.         Log.v("loginactive",String.valueOf(c.getCount()) );
  40.         if(c!=null){
  41.             c.moveToFirst();
  42.             while(!c.isLast()){
  43.                 Log.v("loginactive","outp.");
  44.                 String stud_no=c.getString(0);
  45.                 String stud_name=c.getString(1);
  46.                 System.out.println(stud_no+"---------------"+stud_name);
  47.                 Log.v("loginactive",stud_no+"---------------"+stud_name);
  48.                 c.moveToNext();
  49.             }
  50.         }

  51.     }
  52. }
四、启动DDMS
在eclipse里
Window->New Perspective->DDMS
我学习ophone里使用sqlite3-LMLPHP

五、运行
选择"我学习ophone里使用sqlite3-LMLPHP"里的run configure->如下图填完后->apply->run

我学习ophone里使用sqlite3-LMLPHP

六、查看结果

点击run后马上点击右上角的DDMS就能在log里看到结果

我学习ophone里使用sqlite3-LMLPHP
我学习ophone里使用sqlite3-LMLPHP

七、命令行下查看数据库

Cd $OPHONE_HOME/tools

adb pull /data/data/oms.sqlite_dmoe/databases/testdbtest.db

sqlite3 test.db

我学习ophone里使用sqlite3-LMLPHP

end
09-15 11:45