一、基本使用:
1.数据库的创建:
android为我们提供了一个很方便的抽象帮助类,用于Sqlite数据库的创建,我们只需定义一个类继承该抽象类就能很方便地创建数据库,需要注意的是,如果在数据库中创建两张表,Book和Category,理论上只需要db.execSQL(CREATE_TABLE_CATEGORY), 但是通过sqlite命令查询得知并没有创建成功。其实是因为在onCreate()方法中已经创建了Book表,所以不会再调用onCreate()方法,所以要在onUpdate()方法中调用
db.execSQL(“drop table if exists Book”);
db.execSQL(“drop table if exists Category”);
如果表已经存在,就删除,所以会重新调用onCreate()方法,得到两张表。
public class MyDBHelper extends SQLiteOpenHelper {
private Context mContext;
public static final String CREATE_TABLE_BOOK = "create table Book ("
+ "id integer primary key autoincrement, "
+ "name text, "
+ "pages integer, "
+ "author text, "
+ "prices real)";
public static final String CREATE_TABLE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement, "
+ "category_name text, "
+ "category_code integer)";
public MyDBHelper(Context context) {
super(context, "Book.db", null, 1);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_BOOK);
db.execSQL(CREATE_TABLE_CATEGORY);
Toast.makeText(mContext, "创建成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
}
}
2、用命令行中操作数据库文件
找到adb.exe所在目录,我的在
然后按shift键,鼠标右击,选择在此打开命令窗口;
然后输入adb shell命令,接着输入cd /data/data/包名/databases进入到应用的数据库包中;
然后使用ls命令查看已经创建好的数据库文件;
如果要查看数据库中有几张表,可以先使用sqlite3 数据库名称;
接着输入.table就可以查看数据库中有几张表了
用命令行删除指定文件和文件夹:
rm -r xxx //删除名字为xxx的文件夹及其里面的所有文件
rm xxx //删除文件xxx
rmdir xxx //删除xxx的文件夹
3、表的CRUB操作:
1、增
public void add() {
//使用android提供的api来添加表数据
db = helper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", "The lost man");
values.put("prices", 13.83);
values.put("pages", 293);
values.put("author", "tao");
db.insert("Book", null, values);
// 使用sql语句添加表数据
db.execSQL("insert into Book(name, prices, pages, author) values(?, ?, ?, ?)",
new String[] {"", "", "", ""});
}
2、删
public void delete() {
//使用android提供的api删除表数据
db = helper.getReadableDatabase();
db.delete("Book", "prices>?", new String[]{"100"});
//使用sql语句删除表数据
db.execSQL("delete from Book where prices>?", new String[]{"100"});
}
3、改
public void update() {
//使用android提供的api更新表数据
db = helper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("author", "");
db.update("Book", values, "name=?", new String[]{""});
//使用sql语句更新表数据
db.execSQL("update Book set pages = ? where author=", new String[]{"", ""});
}
4、查
public void query() {
//使用android提供的api查询表数据
db = helper.getReadableDatabase();
Cursor cursor = db.query("Book", null, null, null,
null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
//Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show();
Log.d("name", name);
}
//使用sql语句查询表数据
db.rawQuery("select * from Book", null);
}
二、外部导入数据库:
1、使用raw文件夹导入:
在res目录下新建一个raw目录,将需要使用的数据库文件放在该目录下:
File desFile = new File(getFilesDir(), "address.db");
if(desFile.exists() && desFile.length()>0){
return null;
}
InputStream is = getResources().openRawResource(R.raw.address);
File file = copyFile(is, desFile.getPath());
return file;
private File copyFile(InputStream is, String path) {
File file = new File(path);
byte[] buffer = new byte[1024];
int len ;
try {
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
2、使用assets文件目录:
assets目录是一个和java以及资源文件目录res同级的目录,也是用于存放资源文件的,通常需要自己新建;
private File createDB2() {
File desFile = new File(PATH, "address.db");
InputStream is = null;
try {
is = getAssets().open("address.db");
} catch (IOException e) {
e.printStackTrace();
}
File file = copyFile(is, desFile.getPath());
return file;
}
通过adb我们可以查看到导入数据库成功:
三、数据库的优化:
由于篇幅过大,这个另开一篇文章