安卓 数据库使用_安卓软件开发教程-查字典教程网
安卓 数据库使用
安卓 数据库使用
发布时间:2016-12-09 来源:查字典编辑
摘要:数据库一无所知,最近用到,百度+问群,刚刚入门,自己总结一下做个笔记:1,创建数据库:继承SQLiteOpenHelperpubliccla...

数据库一无所知,最近用到,百度+问群,刚刚入门,自己总结一下做个笔记:

1,创建数据库:继承SQLiteOpenHelper

public class DatabaseHelper extends SQLiteOpenHelper {

	private String Tag = "DatabaseHelper";

	public DatabaseHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		LogUtils.i(Tag, "onCreate");
		try {
			//本机处理操作同步到云端后的记录
			db.execSQL("CREATE TABLE devicesOperation_tb (" 
					+ "directory VARCHAR (50)  NOT NULL," 
					+ "rootDir VARCHAR (20)  NOT NULL," 
					+ "parentDir VARCHAR (20)  NOT NULL,"
					+ "fileName VARCHAR (50)  NOT NULL,"
					+ "time VARCHAR (50)  NOT NULL" 
					+ ");");

			//本机所有文件记录,用于和操作后的对比,devicesFile_tb记录没有,devicesOperation_tb记录有的话,则是删除操作
			db.execSQL("CREATE TABLE devicesFile_tb (" 
					+ "directory VARCHAR (50)  NOT NULL," 
					+ "rootDir VARCHAR (20)  NOT NULL," 
					+ "parentDir VARCHAR (20)  NOT NULL,"
					+ "fileName VARCHAR (50)  NOT NULL,"
					+ "time VARCHAR (50)  NOT NULL" 
					+ ");");

			//云端操作记录表
			db.execSQL("CREATE TABLE serverOperation_tb (" 
					+ "fileName VARCHAR (50)  NOT NULL," 
					+ "rootDir VARCHAR (20)  NOT NULL," 
					+ "parentDir VARCHAR (20)  NOT NULL," 
					+ "operation VARCHAR (10)  NOT NULL," 
					+ "filePath VARCHAR (100)" 
					+ ");");


		} catch (Exception e) {
			// TODO: handle exception
			LogUtils.i(Tag, "onCreate:"+e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
db.execSQL("CREATE TABLE devicesOperation_tb (" 
					+ "directory VARCHAR (50)  NOT NULL," 
					+ "rootDir VARCHAR (20)  NOT NULL," 
					+ "parentDir VARCHAR (20)  NOT NULL,"
					+ "fileName VARCHAR (50)  NOT NULL,"
					+ "time VARCHAR (50)  NOT NULL" 
					+ ");");
这一句是创建数据库, devicesOperation_tb:表名;后面都是字段名,每一句以“,”逗号结束,最后一句不用

2,获取执行创建数据库,得到数据库:

	mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, 1);	
	mSqLiteDatabase = mDatabaseHelper.getReadableDatabase();
只new是得不到数据库的,必须要get ReadableDatabase()或者getWriteableDatabase()一下才能看到数据库,这句执行后,就可以在data/data/你的项目包名/databases下面看到有一个 devicesOperation_tb.db的数据库了

3,数据库添加数据,也就是insert语句:

ContentValues cv = new ContentValues();
cv.put(DevicesOperationTB.directory, f.toString());
cv.put(DevicesOperationTB.rootDir, FilePath.split("/")[0]);
cv.put(DevicesOperationTB.parentDir, FilePath.split("/")[1]);
cv.put(DevicesOperationTB.fileName, FilePath.split("/")[2]);
mSqLiteDatabase.insert(DevicesFileTB.devicesFile_tb, null, cv);

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类