sqlite数据库
创建数据库和表
首先,要写一个类继承SQLiteOpenHelper
在子类的构造方法中创建数据库
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
继承SQliteOpenHelper要覆盖父类的两个方法,直接放上aqi
YoucreateasubclassimplementingonCreate(android.database.sqlite.SQLiteDatabase),onUpgrade(android.database.sqlite.SQLiteDatabase,int,int)
package com.du.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; //操作sqlite数据要继承SQLiteOpenHelper public class MyDbOpenhelper extends SQLiteOpenHelper { public MyDbOpenhelper(Context context) { super(context, "first.db", null, 1); // 创建出数据库 } @Override public void onCreate(SQLiteDatabase db) { // 在数据第一次被创建的时候执行 //创建一张表 db.execSQL("create table student(name varchar(12),age int)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 数据库的版本号 改变的时候会被执行 } }
增删改查
增加
package com.du.db.dao; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.du.db.MyDbOpenhelper; public class StuDao { MyDbOpenhelper openhelp=null; public StuDao(Context context) { openhelp= new MyDbOpenhelper(context); } public void add(String name,int age){ SQLiteDatabase db=openhelp.getWritableDatabase();//获取可写的数据库 ContentValues cv = new ContentValues();//创建一个类似map的容器 cv.put("name", name); cv.put("age",age); db.insert("student", null, cv); db.close(); } }
相关推荐
DAV数据库 2020-06-17
airfling 2020-05-31
zbcaicai 2020-05-26
beibeijia 2020-04-25
Rain 2020-04-16
Plant 2020-04-08
园搬家测试账号 2020-03-25
MFCJCK 2020-02-24
xiaoxiangyu 2020-02-23
Plant 2020-02-03
CharlesYooSky 2020-02-01
MFCJCK 2020-01-31
nxcjh 2020-01-29
wintershii 2020-01-18
zbcaicai 2020-01-03
MFCJCK 2019-12-30
airfling 2019-12-28
Plant 2019-12-27
Dlanguage 2019-12-27
Dlanguage 2019-12-25
whyname 2019-12-23