安卓平台SQLite数据库基础操作总结

时间:2021-01-20 18:10:48

最近学了一些安卓开发,在这里分享一下SQLite数据库的使用相关部分,我使用的工具为Android Studio,后台语言为java:

首先,需要创建一个数据库辅助类DataBaseHelper,用于数据库的创建,并方便我们之后对于数据库链接及的操作:

package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelp extends SQLiteOpenHelper{
    private static final String DATABASENAME ="tip1";//数据库名称
    private static final int DATABASEVERSION =1;//数据库版本
    private static final String TABLENAME="tip1";//表名
    public DataBaseHelp(Context context)//定义构造
    {
    super(context,DATABASENAME,null,DATABASEVERSION);    //调用父类构造
    }
    public void onCreate(SQLiteDatabase db)
    {
        String sql="CREATE TABLE " TABLENAME "(" 
                "id   INTEGER PRIMARY KEY AUTOINCREMENT,"                    //设置自动增长列
                "name  VARCHAR(50) NOT NULL," 
                "text  VARCHAR(50) NOT NULL)";
    db.execSQL(sql);    //执行sql语句
    }
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
    {
        String sql="DROP TABLE IF EXISTS " TABLENAME;
        db.execSQL(sql);
        this.onCreate(db);//创建表

    }
}

  这里在辅助类的构造函数中进行了数据库以及表的创建,在实例化对象时,数据库就会创建出来。

为了操作数据库,接下来要创建一个数据库操作类OperateTable:

package com.example.myapplication;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import java.util.Map;

public class OperateTable {
    private static final String TABLENAME ="tip1";
    private SQLiteDatabase db=null;
    public OperateTable(SQLiteDatabase db)
    {
        this.db=db;
    }
    public void insert(String name,String text)
    {
        String sql="INSERT INTO " TABLENAME " (name,text) VALUES (‘" name "‘,‘" text "‘)";
        this.db.execSQL(sql);


    }
    public void delete(String id)
    {
        String sql="DELETE FROM " TABLENAME " WHERE id=‘" id "‘";
        this.db.execSQL(sql);


    }
    public void updata(String id,String name,String text)
    {
        String sql="UPDATE " TABLENAME " SET name =‘" name "‘,text=‘" text "‘ WHERE id=‘" id "‘";
        this.db.execSQL(sql);
    }
  
        public tip t(String id)
//此为查询语句,tip为我自己设置的一个类,用于获取数据,有String name;String text;两个成员
        {
            tip t=new tip();

            String sql="SELECT name,text FROM " TABLENAME " WHERE id =‘" id "‘";
            Cursor result =this.db.rawQuery(sql,null);
            result.moveToFirst();
            t.setName(result.getString(0));
            t.setText(result.getString(1));
            return t;
        }
}
  

} }

  这两个类添加完毕后,在MainActivity中调用即可

public class MainActivity extends AppCompatActivity {
private OperateTable mytable =null;
private SQLiteOpenHelper helper=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        helper=new DataBaseHelp(this);//实例化数据库辅助类
       
        MainActivity.this.mytable=new
OperateTable(MainActivity.this.helper.getWritableDatabase());//取得可写数据库
MainActivity.this.mytable.insert("第一条","1111111");//执行插入语句

        
    }