首先创建Account实体bean类:
package com.example.sqlitetest; public class Account { private Integer id; private String name; private Integer balance; public Account() { super(); } public Account(String name,Integer balance) { super(); this.name = name; this.balance = balance; } public Account(Integer id,String name,Integer balance) { super(); this.id = id; this.name = name; this.balance = balance; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } @Override public String toString() { return "Account [id=" + id + ",name=" + name + ",balance=" + balance + "]"; } }
再创建MyHelper类继承自sqliteOpenHelper,实现他的构造器和创建数据库以及升级数据库的方法:
package com.example.sqlitetest; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; public class MyHelper extends sqliteOpenHelper { public MyHelper(Context context) { super(context,"test27.db",null,2); } /** * * 在数据库文件创建之后调用 * * * */ @Override public void onCreate(sqliteDatabase db) { // 创建表 db.execsql("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20))"); } /** * * * 数据库版本更新之后调用 * */ @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // 在数据库版本提升之后执行 System.out.println("onUpgrade"); db.execsql("ALTER TABLE account ADD balance INTEGER"); } }
再创建AccountDao类,在他的构造器中,得到MyHelper对象,获得该对象后,既可以通过该对象的getWritableDatabase或者getReadableDatabase方法得到sqliteDatabase,得到sqliteDatabase,既可以通过sqliteDatabase进行增删改查:代码如下:
package com.example.sqlitetest; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; public class AccountDao { MyHelper helper; public AccountDao(Context context) { helper = new MyHelper(context); } public void insert(Account a) { // 获取sqliteDatabase对象 sqliteDatabase db = helper.getWritableDatabase(); // 执行一条sql语句 db.execsql("INSERT INTO account(name,balance) VALUES(?,?)",new Object[] { a.getName(),a.getBalance() }); // 关闭 db.close(); } public void delete(int id) { // 获取sqliteDatabase对象 sqliteDatabase db = helper.getWritableDatabase(); db.execsql("DELETE FROM account WHERE _id=?",new Object[] { id }); db.close(); } public void update(Account a) { // 获取sqliteDatabase对象 sqliteDatabase db = helper.getWritableDatabase(); db.execsql("UPDATE account SET name=?,balance=? WHERE _id=?",a.getBalance(),a.getId() }); db.close(); } public Account query(int id) { sqliteDatabase db = helper.getReadableDatabase(); //执行查询语句,得到结果集 Cursor c = db.rawQuery("SELECT name,balance FROM account WHERE _id=?",new String[]{id + ""}); Account a = null; //判断结果集是否包含下一条数据,如果包含,指针自动向后移动 if(c.moveToNext()){ String name = c.getString(0);//从结果集中获取数据 int balance = c.getInt(1); a = new Account(name,balance);//创建对象 把数据设置到对象中 } return a;//返回对象 } }
最后,测试的方法:
package com.example.sqlitetest; import android.test.AndroidTestCase; public class MyTest extends AndroidTestCase { public void testCreateDB() { new MyHelper(getContext()).getWritableDatabase(); /* * 情况1:数据库文件不存在,创建文件,打开数据库连接(得到sqliteDatabase对象),执行onCreate()方法 * * 情况2:数据库文件存在,版本号没变,打开数据库连接 * * 情况3:数据库文件存在,版本号提升,升级数据库,打开数据库连接,执行onUpgrade()方法 */ } public void testInsert() { Account a = new Account("陈福多",11111111); AccountDao dao = new AccountDao(getContext()); dao.insert(a); } public void testDelete() { AccountDao dao = new AccountDao(getContext()); dao.delete(1); } public void testUpdate() { Account a = new Account(1,"陈福多",100); AccountDao dao = new AccountDao(getContext()); dao.update(a); } public void testQuery() { AccountDao dao = new AccountDao(getContext()); Account a = dao.query(3); System.out.println(a); } }
本案例中所有的数据库的语句:
1.创建表
db.execsql("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20))");
2.更新表
db.execsql("ALTER TABLE account ADD balance INTEGER");
3.插入数据
// 执行一条sql语句 db.execsql("INSERT INTO account(name,a.getBalance() });
4.删除数据
db.execsql("DELETE FROM account WHERE _id=?",new Object[] { id });
db.execsql("UPDATE account SET name=?,a.getId() });6.查询数据
Cursor c = db.rawQuery("SELECT name,new String[]{id + ""});原文链接:https://www.f2er.com/sqlite/200355.html