标签:make sel height query 数据库名 address 库文件 open ack
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//只有在第一次创建数据库文件时才会执行
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql="create table Users (" +
"Id integer primary key autoincrement , " +
"Name text, Age integer ," +
"Phone integer," +
"Address text)";
//执行创建表 SQL
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*
升级数据库
当你想要修改数据表结构,添加数据,删除数据库是,可以将相应SQL写在这里。
接的修改版本号(大于上一次)。
eg:
db.execSQL("drop table if exists Users");
onCreate(db);
*/
}
}
MyDatabaseHelper myDBHepler = new MyDatabaseHelper(this, "DB.db", null, 1);
SQLiteDatabase db = myDBHepler.getWritableDatabase();
//添加数据
db.execSQL("insert into Users(name,age,phone,address) values(?,?,?,?)",
new String[]{"Name1", "18", "123123", "AddressAddress"});
//更新数据
db.execSQL("update Users set Address= ? where name= ?",
new String[]{"AddressUpdated", "Name1"});
//删除数据
db.execSQL("delete fom Users where name= ?", new String[]{"Name2"});
//查询数据
Cursor cursor = db.rawQuery("select * from Users", null);
//获取查询到的数据
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("Name"));
String age = cursor.getString(cursor.getColumnIndex("Age"));
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
Toast.makeText(this, age, Toast.LENGTH_SHORT).show();
}cursor.close();Toast.makeText(this,"Ok", Toast.LENGTH_SHORT).show();
db.close ();
MyDatabaseHelper myDBHepler = new MyDatabaseHelper(this, "DB.db", null, 1);
SQLiteDatabase db = myDBHepler.getWritableDatabase();
// 添加数据
ContentValues valuesInsert = new ContentValues();
valuesInsert.put("Name", "Name1");
valuesInsert.put("Phone", 123123);
valuesInsert.put("Address", "Address Address");
//返回值代表插入新行的IDint insert=db.insert("Users",null,valuesInsert);
//更新数据
ContentValues valuesUpdate=new ContentValues();
valuesUpdate.put("Address","Address UPdated2");
//db.update("Users",values,"Id>2",null);
db.update("Users",valuesUpdate,"Id=?",new String[]{"3"});
//删除数据,返回行代表影响的行数int delete= db.delete("Users","Id=?",new String[]{"4"});
//查询数据
Cursor cusor= db.query("Users",null,null,null,null,null,null);
db.close();
// 开启事务
db.beginTransaction();
try {
db.execSQL("");
db.execSQL("");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
标签:make sel height query 数据库名 address 库文件 open ack
原文地址:http://www.cnblogs.com/-Tiger/p/7137107.html