Sqlite增删改查(一)

前端之家收集整理的这篇文章主要介绍了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,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"},"_id desc");
        for (cusor.moveToFirst(); !cusor.isAfterLast(); cusor.moveToNext()) {
            String id,age,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","name=?",new String[]{mName},null);
        String id,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,age integer not null)");
ContentValues values = new ContentValues();
values.put("name","张三");
values.put("sex","男");
values.put("age",19);
long rowId = db.insert("stutb",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",new String[]{"0"},"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",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,age) values('张三','女',18)"); db.execsql("insert into usertb(name,age) values('李四',19)"); db.execsql("insert into usertb(name,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",null);
db.execsql("create table if not exists usertb (_id integer primary key autoincrement,age integer not null,sex text not null )");
db.execsql("insert into usertb(name,age) values('张三',18)");
db.execsql("insert into usertb(name,age) values('李四',19)");
db.execsql("insert into usertb(name,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",values);
values.clear();
values.put("name","张三丰");
values.put("sex",99);
db.insert("stutb","张三疯");
values.put("sex",59);
db.insert("stutb","张三峰");
values.put("sex",39);
db.insert("stutb","张三封");
values.put("sex",29);
db.insert("stutb",values);
values.clear();
values.put("sex","女");
db.update("stutb","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();
}
}

猜你在找的Sqlite相关文章