sqlite

package com.tarena.day103;


import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;


public class Day16_03_dbActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SQLiteDatabase db = openOrCreateDatabase("stu.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists stutbl("
+ "_id integer primary key autoincrement,"
+ "name text not null," + "sex text not null,"
+ "age integer not null" + ")");


// db.execSQL("insert into stutbl(name,sex,age) values('zs','M',19)");
// db.execSQL("insert into stutbl(name,sex,age) values('Ls','F',16)");
// db.execSQL("insert into stutbl(name,sex,age) values('ww','M',18)");
// db.execSQL("insert into stutbl(name,sex,age) values('cl','F',19)");
//
// db.execSQL("update stutbl set sex='M' where name='Ls'");
//
// db.execSQL("delete from stutbl where _id=4");


String name = "wanglaowu";
String sex = "M";
int age = 58;
// db.execSQL("insert into stutbl(name,sex,age) values('" + name + "','"
// + sex + "'," + age + ")");
ContentValues values = new ContentValues();
values.put("name", name);
values.put("sex", sex);
values.put("age", age);


db.insert("stutbl", null, values);


values.clear();
values.put("sex", "F");
values.put("age", 19);


db.update("stutbl", values, "name like ?", new String[] { "w%" });


db.delete("stutbl", "name like ?", new String[] { "L%" });
// db.delete("stutbl", "name like 'L%'", null);


// Cursor c = db.rawQuery("select * from stutbl where _id>?",
// new String[] { "" + 0 });
Cursor c = db.query("stutbl", new String[] { "_id", "name", "sex",
"age" }, "_id>?", new String[] { "0" }, null, null, "age desc");
// 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", "sex : " + c.getString(c.getColumnIndex("sex")));
// Log.i("info", "age : " + c.getInt(c.getColumnIndex("age")));
// }
// c.close();
// }
if (c != null) {
// c.getCount();
// c.getColumnCount();
// c.getColumnName(0);
// c.getColumnNames();
String[] colNames = c.getColumnNames();
while (c.moveToNext()) {
for (String colName : colNames) {
Log.i("info", colName + ":"
+ c.getString(c.getColumnIndex(colName)));
}
}
c.close();
}


db.close();


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值