SQLite数据库---数据库的增删改查

                 sqlite3工具进行增删改查的检验

在Android开发中,试用真机进行测试无法进入data目录(只有获得Root权限的手机可以进入data目录),因此也无法直接操作应用程序下的数据库。为了解决这个问题,SQLite数据库为开发者提供了一个sqlite3.exe工具,通过这个工具直接可以操作数据库。

   Sqlite3.exe是一个简单的SQLite数据库管理工具,位于Android ADT Eclipse中的sdk/tools目录下。由于Android是在运行时集成了Sqlite3.exe,因此在使用Sqlite3.exe工具之前必须要开启模拟器或者连接真实设备。在使用该工具时,首先需要打开DOS命令行,依次输入如下命令:

1.adb shell(挂载到Linux空间)

2.cd data/data(进入到data/data目录) 

3.cd cn.edu.bzu.databasedemo(应用程序包名)

4.ls (列出当前文件夹下的文件)

5.cd databases(进入databases文件夹)

6.sqlite3 BookStore.db(使用sqlite3操作应用程序下的数据库)

7.select * from book(利用SQL语句查询book表中的信息)

下面通过一个图书案例展示sqlite3工具,关于详细的增删改查代码以及解释,可以参考我的上一篇博文哝。下面只展示代码:

mainactivity.java代码:

package cn.edu.bzu.databasedemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import cn.edu.bzu.databasedemo.DB.DbHelper;

public class MainActivity extends AppCompatActivity {
private DbHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper=new DbHelper(this,2);
    }
    public void createDatabase(View view){
      dbHelper.getWritableDatabase();
    }
    //添加数据
    public void addData(View view)
    {
        SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();//得到一个读写的 SQLiteDatabase 对象
        //将参数名和列添加到ContentValues 对象里面
        ContentValues contentValues=new ContentValues();
        contentValues.put("name","android");
        contentValues.put("author","li");
        contentValues.put("pages",200);
        contentValues.put("price",40);
        //插入
        sqLiteDatabase.insert("book","name",contentValues);
        //insert into book(name) values(null)
        contentValues.clear();//清空
        contentValues.put("name","java");
        contentValues.put("author","hu");
        contentValues.put("pages",321);
        contentValues.put("price",36.5);
        sqLiteDatabase.insert("book","name",contentValues);
    }
    //修改数据
    public  void updateData(View view) {
        //获取一个读写的 SQLiteDatabase 对象
        SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
        //创建一个ContentValues对象
        ContentValues contentValues = new ContentValues();
        //将参数以key,values的形式添加进去
        contentValues.put("price", 1000);
        //执行修改的方法
        sqLiteDatabase.update("book", contentValues, "name=?", new String[]{"java"});
    }
//删除数据
       public void deleteData(View view){
        SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
        sqLiteDatabase.delete("book","pages<?",new String[]{"300"});

    }
    //查询数据
    public void queryData(View view)
    {
        StringBuilder stringBuilder=new StringBuilder();
        SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
        Cursor cursor=sqLiteDatabase.query("book",null,null,null,null,null,null);//表示查询的是所有数据
        if(cursor.moveToFirst())
        {
            do{
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String author=cursor.getString(cursor.getColumnIndex("author"));
               int pages=cursor.getInt(cursor.getColumnIndex("pages"));
                double price=cursor.getDouble(cursor.getColumnIndex("price"));
               stringBuilder.append(name+"-"+author+"-"+pages+"-"+price);
            }while(cursor.moveToNext());//是否有下一条值
        }
        //当需要操作很长的字符串,或者要对字符串进行非常频繁的操作时,应该使用StringBuilder,其余场合,用String比较方便。
        Toast.makeText(this,stringBuilder.toString(),Toast.LENGTH_LONG).show();
    }
}
DBHelper代码:

package cn.edu.bzu.databasedemo.DB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DbHelper extends SQLiteOpenHelper{
    private  Context context;//成员变量
    public static final String DB_NAME="BookStore.db";
    public static final  String CREATE_BOOK="create table book(id integer primary key autoincrement," +
            "author text,price real,pages integer,name text)";//autoincrement,用于主键自动增长
    public  static final  String CREATE_CATEGORY="create table category(id integer primary key autoincrement,name text,code integer)";
    public DbHelper(Context context, int version) {
        super(context,DB_NAME, null, version);//上下文,数据库名,游标工厂,默认为null,数据库版本
        Log.d("DBHelper","constructor");
        this.context=context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("DBHelper","onCreate");
 //建表
        db.execSQL(CREATE_BOOK);
        //弹出toast表示执行完毕
        db.execSQL(CREATE_CATEGORY);
        Toast.makeText(context,"create success",Toast.LENGTH_LONG).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");//先把旧表删除
        onCreate(db);//再建表
    }
}
activity_main.html代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.databasedemo.MainActivity">

    <Button
        android:text="create  database"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:onClick="createDatabase"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="add  data"
        android:onClick="addData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="update  data"
        android:onClick="updateData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="delete  data"
        android:onClick="deleteData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="query  database"
        android:onClick="queryData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_below="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

                 

                             效果图

代码完成后,运行程序,需要打开dos命令,运用sqlite3工具检验数据库的操作。

     

                依次输入上述命令进行查询

     

           依次在模拟器上点击按钮,继续查询表,观察字段变化

                 

        

 这样,数据库的基本操作就完成了。但是,我这个项目存在着一些漏洞,dos命令中出现了乱码,而且出现多条数据,这个问题我还没有解决。嘻嘻,请教大神们。

 



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值