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();
}
}
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();
}
}