写得很辛苦,大家觉得好的话请多多回帖支持我,有时间会发布第二篇.
演示环境
Windows xp
Eclipse 3.4.2
Ophone SDK 1.0
一、 创建工程
在Eclipse里
File->new->Android project->输入工程名sqlite_demo等等如下图:->点击完成
二、建立数据库连接类
在src下oms.sqlite_demo下新建DBHelper类
写代码如下
- /*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();
-
}
-
}
-
-
}
-
}
四、启动DDMS
在eclipse里
Window->New Perspective->DDMS
五、运行
选择""里的run configure->如下图填完后->apply->run
六、查看结果
点击run后马上点击右上角的DDMS就能在log里看到结果
七、命令行下查看数据库
Cd $OPHONE_HOME/tools
adb pull /data/data/oms.sqlite_dmoe/databases/testdb
test.db
sqlite3 test.db
end
阅读(1610) | 评论(0) | 转发(0) |