今晚写了个sqLite的应用例子,粘一下代码,就不详细详述了。
package com.peidw.demo;
import java.io.FileNotFoundException;
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 peidw 2009-04-06
*
*/
public class DatabaseHelper extends SQLiteOpenHelper{
private Context ctx = null;
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
package com.peidw.demo;
import java.util.*;
import android.app.*;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.*;
import android.util.*;
/**
* 操作数据库例子
* @author peidw 2009-04-19
*
*/
public class ConnectionProvider {
private static SQLiteDatabase db=null;
public ConnectionProvider(Context ctx) {
if (db==null){
Log.v("ConnectionProvider","ctx="+ctx.toString());
DatabaseHelper dbHelper=new DatabaseHelper(ctx,"StudDB", null,1);
db= dbHelper.getWritableDatabase();
}
}
public SQLiteDatabase getConnection (){
return db;
}
public void closeConnection (){
db.close();
}
/**
* 判断表是否存在
* @param tablename
* @return
*/
public boolean isTableExits(String tablename){
boolean result=false;//表示不存在
String str="select count(*) xcount from sqlite_master where table='"+tablename+"'";
Cursor c = db.rawQuery(str,null);
int xcount=c.getColumnIndex("xcount");
if(xcount!=0){
result=true; //表存在
}
return result;
}
}
在程序中操作数所的代码片段
ConnectionProvider cp=new ConnectionProvider(this.getBaseContext());
// if(cp.isTableExits(TABLE_NAME)){//表已存在
createTable(cp);
queryTable(cp);
// }else{//表不存在
// createTable();
// }
public void createTable( ConnectionProvider cp){
SQLiteDatabase db= cp.getConnection();
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(ConnectionProvider cp){
String str="select * from "+TABLE_NAME;
Cursor c =cp.getConnection().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();
}
}
}
麻烦帮单击一次广告了 !!!
posted on 2009-04-19 22:20
有猫相伴的日子 阅读(7814)
评论(4) 编辑 收藏 所属分类:
Android