android中的数据库按存储位置分为两种:1,系统目录下的数据库 ;2,sdcard下数据库
首先来说系统目录下的数据库:
一般位置:/data/data/APK包名/databases/xx.db (xx 数据库名称)
看下下面的类:
import android.content.Context; //引入相关包
import android.database.sqlite.SQLiteDatabase; //引入相关包
import android.database.sqlite.SQLiteOpenHelper; //引入相关包
import android.database.sqlite.SQLiteDatabase.CursorFactory;//引入相关包
public class SqLiteDBHelper extends SQLiteOpenHelper{
public SqLiteDBHelper(Context context, String name, CursorFactory factory,
int version) {//继承SQLiteOpenHelper的类,必须有该构造函数
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库时调用,此方法是在调用了getReadableDatabase()或getWritableDatabase()后才调用
db.execSQL("create table sqlitetest(uid long,uname varchar(25))");
System.out.println("already create a database:sqlitetest.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//升级数据库时掉用
}
}
该类是创建此类数据库的一个简单方法
//创建数据库
SqLiteDBHelper dh=new SqLiteDBHelper(MyActivity.this,"testdb",null,1);
System.out.println("create or open database success!");
SQLiteDatabase sld=dh.getReadableDatabase(); //得到一个SQLiteDatabase对象,用于操控数据库
Toast.makeText(MyActivity.this, "创建或打开数据库", Toast.LENGTH_SHORT).show();
//增加数据库记录
ContentValues cv=new ContentValues();//得到ContentValues对象
cv.put("uid", 1); //放入键值对,键要与列名一致,值要与列的数据类型一致
cv.put("uname", "zcl"); //放入键值对,键要与列名一致,值要与列的数据类型一致
SqLiteDBHelper dh=new SqLiteDBHelper(MyActivity.this,"testdb",null,1);//创建数据库
Toast.makeText(MyActivity.this, "插入记录", Toast.LENGTH_SHORT).show();
SQLiteDatabase sld=dh.getWritableDatabase(); //得到一个SQLiteDatabase对象,用于操控数据库
sld.insert("sqlitetest", null, cv);//增加数据库记录
System.out.println("success insert a new content!");
//更新数据库记录
SqLiteDBHelper dh=new SqLiteDBHelper(MyActivity.this,"testdb",null,1);//创建数据库
SQLiteDatabase sld=dh.getWritableDatabase();//得到一个SQLiteDatabase对象,用于操控数据库
ContentValues cv = new ContentValues(); //得到ContentValues对象
Toast.makeText(MyActivity.this, "更新记录", Toast.LENGTH_SHORT).show();
cv.put("uname", "zcl_update");
sld.update("sqlitetest", cv, "uid=?", new String[]{"1"});//更新数据库记录
System.out.println("success updata the content!");
//查询记录
SqLiteDBHelper dh=new SqLiteDBHelper(MyActivity.this,"testdb",null,1);//创建数据库
Toast.makeText(MyActivity.this, "查询记录", Toast.LENGTH_SHORT).show();
SQLiteDatabase sld=dh.getReadableDatabase(); //得到一个SQLiteDatabase对象,用于操控数据库
Cursor cursor=sld.query("sqlitetest", new String[]{"uid","uname"}, "uid=?", new String[]{"1"}, null, null, null);
while(cursor.moveToNext()){
//打印输出
String name=cursor.getString(cursor.getColumnIndex("uname"));
System.out.println("query result:"+name);
}}});
sdcard下数据库
位置:需要指定sdcard中的路径
此处用:path="/sdcard/wop/data/";
/**
* @Description: TODO(判断sqlite是否已经打开)
*/
private void checkSqlite() {
if (sqlite == null || !sqlite.isOpen()) {
sqlite = SQLiteDatabase.openDatabase(strpath, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
}
/**
* @Description: TODO(设置事务处理成功)
*/
private void transactionSuccessful() {
sqlite.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
sqlite.endTransaction(); // 处理完成
}
1)创建数据库:
此时的创建其实就是将原来已经创建好的db文件copy入sdcard中
而在工程中db文件的位置最好在asset下,如wop。db3
然后根据文件复制的方式,执行copy
2)增
在原wop。db3文件中进行添加字段;推荐SQLite Expert Profession工具
3)删
// 删除photo图片
public Boolean deletePhoto(String strphotopath) {
boolean result = false;
// 插入tag表
try {
checkSqlite();
// 插入table_Day表
sqlite.beginTransaction(); // 手动设置开始事务
sqlite.execSQL("delete from table_Photo where photo_url='"
+ strphotopath + "';");
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
transactionSuccessful();
}
return result;
}
4)改
public boolean updateprivate(String strserver_id, String strlocalzt,
String Strpicasa_uploaded) {
boolean result = false;
try {
checkSqlite();
String sql = "update table_Photo set server_photo_id='"
+ strserver_id + "',picasa_uploaded='" + Strpicasa_uploaded
+ "',localzt='" + strlocalzt
+ "' where _id=(SELECT max(_id) FROM table_Photo);";
sqlite.execSQL(sql);
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}
5)查
/**
* 获取最后一张图片路径
*/
public String getLastPhotoPath() {
Cursor curPhone = null;
String PhotoPath = null;
try {
checkSqlite();
String sql = "Select max(_id),photo_url From table_Photo ORDER BY _id desc;";
curPhone = sqlite.rawQuery(sql, null);
if (curPhone.getCount() > 0) {
while (curPhone.moveToNext()) {
PhotoPath = curPhone.getString(curPhone
.getColumnIndex("photo_url"));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
item = null;
PhotoPath = null;
} finally {
if (curPhone != null) {
curPhone.close();
}
}
return PhotoPath;
}