Android数据的四种存储方式之SQLite数据库
/**
* 本例解决的问题:
* 核心问题:通过SQLiteOpenHelper类创建数据库对象
* 通过数据库对象对数据库的数据的操作
* 1.sql语句方式操作SQLite数据库
* 2.谷歌提供的api对SQLite数据库的操作
* 3.SQLite对事务的操作
*/
import com.ghsy.createsqlitedb.db.MyOpenHelper;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase {
MyOpenHelper oh;
SQLiteDatabase db;
public void test() {
// 创建一个MyOpenHelper对象
// 改动此处的版本,会运行upgrade方法--upgrade方法中加入?了一列
MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 3);
// 假设数据库不存在,先创建数据库,再打开数据库,假设已经存在,直接打开
SQLiteDatabase db = oh.getWritableDatabase();
db.close();
}
// 測试框架初始化完毕
/**
* This method is called before a test is executed
*/
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
oh = new MyOpenHelper(getContext(), "people.db", null, 3);
db = oh.getWritableDatabase();
}
|
public void insert() {
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "大明", "18666", 6000 });
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "小红", "18666", 6000 });
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "大红", "18666", 6000 });
}
public void delete() {
db.execSQL("delete from person where name = ?", new Object[] { "大明" });
}
public void update() {
db.execSQL("update person set money = 10000 where name = ?",
new Object[] { "小明" });
}
public void query() {
// 游标,存放查询返回的数据,获取方法跟resultSet高度雷同
Cursor c = db.rawQuery("select * from person", new String[] {});
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndex("name"));
String phone = c.getString(c.getColumnIndex("phone"));
System.out.println(name + ";" + phone);
}
}
|
/**
* api-insert data to table
*/
public void insertApi() {
ContentValues cv = new ContentValues();
cv.put("name", "微明");
cv.put("phone", 15666);
cv.put("money", 630);
long id = db.insert("person", null, cv);
System.out.println(id);
}
/**
* api-delete data from table
*
* @return the number of rows affected
*/
public int deleteApi() {
int affectedNum = db.delete("person", "name=?", new String[] { "小红" });
return affectedNum;
}
/**
* api-update
*/
public void updateApi() {
ContentValues contentValues = new ContentValues();
contentValues.put("name", "小红");
contentValues.put("money", "10");
// return the number of rows affected
db.update("person", contentValues, "name=?", new String[] { "大红" });
}
public void queryApi() {
Cursor cursor = db.query("person", new String[] { "phone", "money" },
null, null, null, null, null);
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String money = cursor.getString(cursor.getColumnIndex("money"));
System.out.println(phone + "##" + money);
}
}
|
/**
* 银行转账操作
*/
public void transation(){
db.beginTransaction();
try {
//name为微明的用户向小红转账
ContentValues contentValues=new ContentValues();
contentValues.put("money", 1000);
db.update("person", contentValues, "name=?", new String[]{"微明"});
ContentValues contentValues2=new ContentValues();
contentValues2.put("money", 1100);
db.update("person", contentValues2, "name=?", new String[]{"小红"});
//全部语句运行完毕,若没有异常,则会运行这句设置事务成功的标记
db.setTransactionSuccessful();
} finally {
//会检查事务的标识,若没有调用setTransactionSuccessful()方法设置标志,则回滚事务。否则提交事务。
db.endTransaction();
}
}
}
MyOpenHelper.java
**SQLiteOpenHelper:
* A helper class to manage database creation and version management.
* 所以,SQLiteOpenHelper是对库本身的操作。若要对库中数据操作,须要使用库对象的方法。
*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
//name:数据库文件的名字
//factory:游标工厂
//version:版本号,必须大于等于1
public MyOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
//数据库创建时调用
@Override
public void onCreate(SQLiteDatabase db) {
//创建一个person表
db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20))");
System.out.println("oncreate调用了");
}
//数据库升级时调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("onupgrade调用了");
db.execSQL("alter table person add money char(20)");
}
}
|

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
当ASP.NET Forms验证方式遭遇苹果IOS
一、问题出现 我在用ASP.NET MVC4做微信开发的时候,用Forms验证方式做为authentication。 一般都是在 web.config加: <authentication mode="Forms" > <forms loginUrl="~/Account/Login" name="webcookies" slidingExpiration="true" timeout="30" /> </authentication> 然后用户登录成功后就设置Cookies,代码如下: public static void SetTicket(HttpResponseBase response, bool remeberMe, int version, string identity, string displayName) { FormsAuthentication.SetAuthCookie(identity, remeberMe); string userData = displayName; var authTicket = new Form...
-
下一篇
谷歌反垄断调查起伏不断
国际科技巨头最头疼的就是反垄断调查,昨日消息,负责调查Alphabet旗下谷歌公司是否存在滥用市场地位行为的欧盟反垄断监管部门明确表示,目前并没有将调查延伸到该公司与欧盟成员国所签署的纳税协议的范畴。然而,韩国反垄断机构公平贸易委员会却表示,正在对谷歌与智能手机制造商签署的协议进行密切审查。 欧盟反垄断委员会主席玛格丽特·韦斯塔格指出,虽然苏格兰民族党在今年1月曾向欧盟写信反映了谷歌与英格兰税务部门签订的1.3亿欧元税收协定存在令人担忧的问题,但该党目前尚未正式向递交书面起诉文件。尽管韦斯塔格仍不能明确告知一个作出最终裁决的时间,但她强调谷歌的案子将拥有绝对高的优先层级。 欧盟对于谷歌的调查如今仍专注于2010年启动的反垄断层面。谷歌在欧洲地区被指控在搜索结果中偏向优先服务自家产品。尤其是在一些在线搜索广告中,谷歌更是存在屏蔽竞争对手产品,以凸显自己的行为,Android操作系统中也延续了这种不公平的策略。欧盟委员会已经对谷歌发出警告,并要求后者更正其商业行为。 就在消息放出的同一天,韩国反垄断机构公平贸易委员会主席Jeong Jae-chan表示,该机构正在对谷歌与智能手机制造商签署...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7,8上快速安装Gitea,搭建Git服务器