SQLiteDatabase
2016-05-17 20:41
399 查看
public class MyHelper extends SQLiteOpenHelper { public MyHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table account(id integer primary key autoincrement,name varchar(20),acc varchar(20),money float)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public class MainActivity extends Activity { private SQLiteDatabase sql; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sql = new MyHelper(this, "accountData", null, 1).getReadableDatabase(); ContentValues cv1 = new ContentValues(); cv1.put("name", "哈登"); cv1.put("acc", "123123123123123"); cv1.put("money", "1000.00f"); sql.insert("account", null, cv1); ContentValues cv2 = new ContentValues(); cv2.put("name", "霍华德"); cv2.put("acc", "456456456456456"); cv2.put("money", "2000.00f"); sql.insert("account", null, cv2); // 开启事务 sql.beginTransaction(); ContentValues from = new ContentValues(); from.put("money", "800.00f"); sql.update("account", from, "acc=?", new String[] { "123123123123123" }); ContentValues to = new ContentValues(); to.put("money", "2200.00f"); sql.update("account", to, "acc=?", new String[] { "456456456456456" }); sql.setTransactionSuccessful(); sql.endTransaction(); } } //查询 cursor = sqd.rawQuery("select * from useraddr", null); l = new ArrayList(); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String tel = cursor.getString(cursor.getColumnIndex("tel")); String addr = cursor.getString(cursor.getColumnIndex("addr")); l.add(new User(name, tel, addr)); } //添加 sqd.execSQL("insert into useraddr(name,tel,addr) values('"+ name + "','" + tel + "','" + addr + "')"); //刷新 sqd.execSQL("update user set age=18 where name like ?",new Object[]{"%zhang%"}); //删除 sqd.execSQL("delete from useraddr where name='"+ i.getStringExtra("name") + "'");
相关文章推荐
- 数据库理论-设计范式
- 数据库——范式
- 转:windows下定时执行备份数据库
- mysql安装
- windows如何安装和配置mongodb
- SQL之视图的使用
- MySql乱码的解决方案
- H2数据库使用
- MySQL锁定状态查看命令
- Mysql命令大全
- MySQL中concat函数
- mysql的一些查询优化,count优化,limit优化
- TCMalloc 对MySQL 性能 优化的分析
- SQL性能优化
- 漫谈数据库索引
- 数据库的原理,一篇文章搞定(三)
- 数据库的原理,一篇文章搞定(二)
- 数据库的原理,一篇文章搞定(一)
- ubuntu14.04 mongodb c++ driver安装
- SQL中小技巧代码