SQLite 基本操作(增删改查)

当你还不能写出自己满意的程序时,你就不要去睡觉。
public class DBOpenHelper extends SQLiteOpenHelper{
public DBOpenHelper(Context context, String name) {
super(context, name, null, 1);
// TODO Auto-generated constructor stub
}
public DBOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override//首次创建数据库的时候调用 一般可以把建库 建表的操作
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
db.execSQL("insert into stutb(name,sex,age)values('张三','女',18)");
}
@Override//当数据库的版本发生变化的时候 会自动执行
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

查找:
 private void queryall(SQLiteDatabase db) {
       Cursor  cusor = db.query("stutb", new String[]{"name", "sex", "age"}, null, null, null, null, "_id  desc");
        for (cusor.moveToFirst(); !cusor.isAfterLast(); cusor.moveToNext()) {
            String id, age,name, sex;
            id = cusor.getLong(cusor.getColumnIndex("id"))+"";
            age = cusor.getLong(cusor.getColumnIndex("age"))+"";
            name = cusor.getString(cusor.getColumnIndex("name"));
            sex = cusor.getString(cusor.getColumnIndex("sex"));
            Log.i("info", "id:  " + id + "name" + name + "sex" + sex + "age" + age);
        }
        cusor.close();


    }


    private void qureyByName(String mName, SQLiteDatabase db) {
        Cursor  cusor = db.query("stutb",null, "name=?", new String[]{mName}, null, null, null);
        String id, age,name, sex;
        if (cusor.moveToFirst()) {
            id = cusor.getLong(cusor.getColumnIndex("id"))+"";
            age = cusor.getLong(cusor.getColumnIndex("age"))+"";
            name = cusor.getString(cusor.getColumnIndex("name"));
            sex = cusor.getString(cusor.getColumnIndex("sex"));
            Toast.makeText(this, id + name + sex + age + "", Toast.LENGTH_LONG).show();
            Log.i("info", "id:  " + id + "name" + name + "sex" + sex + "age" + age);
        } else {
            Toast.makeText(this, "no query  result", Toast.LENGTH_LONG).show();
        }
    }


增加: SQLiteDatabase db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
ContentValues values = new ContentValues();
values.put("name", "张三");
values.put("sex", "男");
values.put("age", 19);
long rowId = db.insert("stutb", null, values);
values.clear();


删除:
db.update("stutb", values, "_id>?", new String[]{"3"});//将全部id>3的人的性别改成女
db.delete("stutb", "name like ?", new String[]{"%丰%"});//删除所有名字中带有丰的人
Cursor c = db.query("stutb", null, "_id>?", new String[]{"0"}, null, null, "name");
if (c!=null) {
String [] columns= c.getColumnNames();
while (c.moveToNext()) {
for (String columnName : columns) {
   Log.i("info", c.getString(c.getColumnIndex(columnName)));
}
}
c.close();
}
db.close();
public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//每个程序都有自己的数据库 默认情况下是各自互相不干扰
//创建一个数据库 并且打开
SQLiteDatabase db =   openOrCreateDatabase("user.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )");
db.execSQL("insert into usertb(name,sex,age) values('张三','女',18)");
db.execSQL("insert into usertb(name,sex,age) values('李四','女',19)");
db.execSQL("insert into usertb(name,sex,age) values('王五','男',20)");

Cursor c = db.rawQuery("select * from usertb", null);
if (c!=null) {
while (c.moveToNext()) {
Log.i("info", "_id:"+c.getInt(c.getColumnIndex("_id")));
Log.i("info", "name:"+c.getString(c.getColumnIndex("name")));
Log.i("info", "age:"+c.getInt(c.getColumnIndex("age")));
Log.i("info", "sex:"+c.getString(c.getColumnIndex("sex")));
Log.i("info", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
c.close();
}
db.close();
}



public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//每个程序都有自己的数据库 默认情况下是各自互相不干扰
//创建一个数据库 并且打开
SQLiteDatabase db =   openOrCreateDatabase("user.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )");
db.execSQL("insert into usertb(name,sex,age) values('张三','女',18)");
db.execSQL("insert into usertb(name,sex,age) values('李四','女',19)");
db.execSQL("insert into usertb(name,sex,age) values('王五','男',20)");

Cursor c = db.rawQuery("select * from usertb", null);
if (c!=null) {
while (c.moveToNext()) {
Log.i("info", "_id:"+c.getInt(c.getColumnIndex("_id")));
Log.i("info", "name:"+c.getString(c.getColumnIndex("name")));
Log.i("info", "age:"+c.getInt(c.getColumnIndex("age")));
Log.i("info", "sex:"+c.getString(c.getColumnIndex("sex")));
Log.i("info", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
c.close();
}
db.close();
}


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
ContentValues values = new ContentValues();
values.put("name", "张三");
values.put("sex", "男");
values.put("age", 19);
long rowId = db.insert("stutb", null, values);
values.clear();
values.put("name", "张三丰");
values.put("sex", "男");
values.put("age", 99);
db.insert("stutb", null, values);
values.clear();
values.put("name", "张三疯");
values.put("sex", "男");
values.put("age", 59);
db.insert("stutb", null, values);
values.clear();
values.put("name", "张三峰");
values.put("sex", "男");
values.put("age", 39);
db.insert("stutb", null, values);
values.clear();
values.put("name", "张三封");
values.put("sex", "男");
values.put("age", 29);
db.insert("stutb", null, values);
values.clear();
values.put("sex", "女");
db.update("stutb", values, "_id>?", new String[]{"3"});//将全部id>3的人的性别改成女
db.delete("stutb", "name like ?", new String[]{"%丰%"});//删除所有名字中带有丰的人
Cursor c = db.query("stutb", null, "_id>?", new String[]{"0"}, null, null, "name");
if (c!=null) {
String [] columns= c.getColumnNames();
while (c.moveToNext()) {
for (String columnName : columns) {
Log.i("info", c.getString(c.getColumnIndex(columnName)));
}
}
c.close();
}
db.close();
}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值