Sqlite数据库的使用

一、基本使用:

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我们可以查看到导入数据库成功:
这里写图片描述

三、数据库的优化:

由于篇幅过大,这个另开一篇文章

参考文章:
adb shell下使用命令行删除android系统中指定文件夹和文件

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值