android 中数据库SQLite使用

首先要定义一个DBHelper extends SQLiteOpenHelper 里面可以定义一些基本操作

只有通过SQLiteOpenHelper才能访问数据库的


DBHelper database;
database = new DBHelper(Culture.this,"data");
SQLiteDatabase db = database.getWritableDatabase();
db.execSQL("create table if not exists company(id integer primary key autoincrement,name varchar(20),introduce text,selfinfo text,culture text)");
ContentValues contentValues = new ContentValues();
contentValues.put("id",1);
contentValues.put("name","花旗");
contentValues.put("introduce", "花旗银行是花旗集团属下的一家零售银行");
contentValues.put("selfinfo", "公司名称:花旗银行 ");
contentValues.put("culture", "以人为本   客户至上  寻求创新");
db.insert("company", null, contentValues);
db.close();
上面是创建表和数据库,用ContentValues键值对来对数据库进行插入
DBHelper database;
database = new DBHelper(CultureInfo.this,"data");
SQLiteDatabase db = database.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from company", null);
cursor.moveToFirst();
cursor.getInt(0)        //返回的第一列的数据id
cursor.getString(1);    //返回的是name列的数据
cursor.close();
db.close();
在用cursor访问数据时记住要将cursor的指针moveToFirst()这使得cursor查询数据时从第一行开始查询的
如果想要查询第二行时则moveToNext()
while(cursor.moveToNext()) {

  对每一行数据进行访问

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值