一. 项目简介
基于Android平台开发的一款智能自然语言交流APP,可以回答用户提出的问题,同时用户可以教它说话。通过webservice调用复旦nlp分词算法,将分解的词语与SQLite数据库中的语料匹配,找到合适答案反馈给用户。
二. 我的职责
负责SQLite数据库表结构的设计以及如何在程序中访问数据库。
三. Android如何使用SQLite数据库
DbAdapter类负责Android客户端与SQLite数据库适配
package upc.atom.xianshi;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbAdapter extends SQLiteOpenHelper{
private static final int VERSION = 1;
private static final String DBNAME = "test.db";
public DbAdapter(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public DbAdapter(Context context,String name){
this(context,name,VERSION);
}
public DbAdapter(Context context){
this(context,DBNAME,VERSION);
}
public DbAdapter(Context context,String name,int version){
this(context,name,null,VERSION);
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("update a database");
}
}
四. SQLite数据库的增加查找
DbAdapterdbHelper = new DbAdapter(AtomStudy.this,"test.db");
//只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
SQLiteDatabase db =dbHelper.getWritableDatabase();
增加:
db.execSQL("insertinto noun_sheet(id,nounName) values(?,?)", newObject[]{ReturnObjMaxRowid(),myQuestion.getText().toString()});
查找:
DbAdapterdbHelper = new DbAdapter(AtomXianshi.this,"test.db");
//只有调用了DatabaseHelper对象的
//getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
SQLiteDatabase db =dbHelper.getReadableDatabase();
cursor1 =db.query("r_sheet", new String[]{"id"},"charName=?", new String[]{mySubject}, null, null, null);
if(cursor1.moveToNext()){//在r表中查询
id_temp1=cursor1.getInt(cursor1.getColumnIndex("id"));
}
五. 如何将数据库拷贝到手机中
//数据库导入手机
public void CreateSdcardDatabase(){
String DB_PATH = "/data/data/upc.atom.xianshi/databases/";
String DB_NAME = "test.db";
if ((new File(DB_PATH + DB_NAME)).exists() == false) {
// 如 SQLite 数据库文件不存在,再检查一下 database 目录是否存在
File f = new File(DB_PATH);
// 如 database 目录不存在,新建该目录
if (!f.exists()) {
f.mkdir();
}
try {
// 得到 assets 目录下我们实现准备好的 SQLite 数据库作为输入流
InputStream is = getBaseContext().getAssets().open(DB_NAME);
// 输出流
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
// 文件写入
byte[] buffer = new byte[78848];
int length=0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
// 关闭文件流
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
应用截图
源码下载:http://download.csdn.net/detail/lantiancaiyun/7684515
https://github.com/zhiruchen/Atom
参考博客:http://blog.csdn.net/liuhe688/article/details/6715983