Android SQLite数据库
SQLite数据库
SQLiteDatabase //管理操作数据库
管理
openDatabase //打开
openOrCreateDatabase //打开或创建
操作 增删改查
execSQL //执行SQL语句
insert //增
update //改
delete //删
query //查
rawQuery //sql语句查
事物
beginTransaction //开始
endTransaction //结束
Cursor //查询结果
move
SQLiteOpenHelper //管理数据库和版本更新
onCreate //第一次创建数据库回调
onUpgrade //数据库版本更新时回调
getReadableDatabase //读的方式打开
getWritableDatabase //写的方式打开
close //关闭所有打开的SQLiteDatabase
Demo1 插入和查询数据
public class DBTest extends Activity {
SQLiteDatabase db;
Button bn = null;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initTitle();
initData();
initView();
}
public void initTitle() {
}
public void initData() {
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);
try {
db.execSQL("create table news_inf(_id integer" + " primary key autoincrement," + " news_title varchar(50),"
+ " news_content varchar(255))");
} catch (Exception e) {
Log.i("TAG", "e---" + e);
}
}
public void initView() {
listView = (ListView) findViewById(R.id.show);
bn = (Button) findViewById(R.id.ok);
queryData();
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View source) {
String title = ((EditText) findViewById(R.id.title)).getText().toString();
String content = ((EditText) findViewById(R.id.content)).getText().toString();
try {
// 插入数据
insertData(db, title, content);
queryData();
} catch (SQLiteException se) {
}
}
});
}
// 插入数据
private void insertData(SQLiteDatabase db, String title, String content) {
// 执行插入语句
db.execSQL("insert into news_inf values(null , ? , ?)", new String[] { title, content });
}
// 查询数据
private void queryData() {
Cursor cursor = db.rawQuery("select * from news_inf", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(DBTest.this, R.layout.line, cursor, new String[] {
"news_title", "news_content" }, new int[] { R.id.my_title, R.id.my_content },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
}
@Override
public void onDestroy() {
super.onDestroy();
// 退出程序时关闭SQLiteDatabase
if (db != null && db.isOpen()) {
db.close();
}
}
}
Demo2 单词本
public class Dict extends Activity implements View.OnClickListener{
MyDatabaseHelper dbHelper;
Button insert = null;
Button search = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initTitle();
initData();
initView();
}
public void initTitle() {
}
public void initData() {
// 创建MyDatabaseHelper对象,指定数据库版本为1,此处使用相对路径即可,
// 数据库文件自动会保存在程序的数据文件夹的databases目录下。
dbHelper = new MyDatabaseHelper(this, "myDict.db3", 1);
}
public void initView() {
insert = (Button) findViewById(R.id.insert);
search = (Button) findViewById(R.id.search);
insert.setOnClickListener(this);
search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:// 插入
String word = ((EditText) findViewById(R.id.word)).getText().toString();
String detail = ((EditText) findViewById(R.id.detail)).getText().toString();
insertData(dbHelper.getReadableDatabase(), word, detail);
break;
case R.id.search:// 搜索
// 获取用户输入
String key = ((EditText) findViewById(R.id.key)).getText().toString();
// 执行查询
Cursor cursor = dbHelper.getReadableDatabase().rawQuery(
"select * from dict where word like ? or detail like ?",
new String[] { "%" + key + "%", "%" + key + "%" });
Bundle data = new Bundle();
data.putSerializable("data", converCursorToList(cursor));
Intent intent = new Intent(Dict.this, ResultActivity.class);
intent.putExtras(data);
startActivity(intent);
break;
default:
break;
}
}
//插入
private void insertData(SQLiteDatabase db, String word, String detail) {
db.execSQL("insert into dict values(null , ? , ?)", new String[] { word, detail });
Toast.makeText(Dict.this, "添加生词成功!", 8000).show();
}
//查询结果转集合
protected ArrayList<Map<String, String>> converCursorToList(Cursor cursor) {
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
// 遍历Cursor结果集
while (cursor.moveToNext()) {
// 将结果集中的数据存入ArrayList中
Map<String, String> map = new HashMap<String, String>();
// 取出查询记录中第2列、第3列的值
map.put("word", cursor.getString(1));
map.put("detail", cursor.getString(2));
result.add(map);
}
return result;
}
@Override
public void onDestroy() {
super.onDestroy();
// 退出程序时关闭MyDatabaseHelper里的SQLiteDatabase
if (dbHelper != null) {
dbHelper.close();
}
}
}
public class MyDatabaseHelper extends SQLiteOpenHelper {
final String CREATE_TABLE_SQL = "create table dict(_id integer primary " + "key autoincrement , word , detail)";
public MyDatabaseHelper(Context context, String name, int version) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 第一次使用数据库时自动建表
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("--------onUpdate Called--------" + oldVersion + "--->" + newVersion);
}
}
public class ResultActivity extends Activity {
List<Map<String, String>> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
initTitle();
initData();
initView();
}
public void initTitle() {
}
public void initData() {
list = (List<Map<String, String>>) getIntent().getExtras().getSerializable("data");
}
public void initView() {
ListView listView = (ListView) findViewById(R.id.show);
SimpleAdapter adapter = new SimpleAdapter(ResultActivity.this, list, R.layout.line, new String[] { "word",
"detail" }, new int[] { R.id.word, R.id.detail });
listView.setAdapter(adapter);
}
}
Code见 https://github.com/huanyi0723/SQLiteDemo/
Android SQLite数据库的更多相关文章
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- Android SQLite 数据库详细介绍
Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...
- Android sqlite数据库存取图片信息
Android sqlite数据库存取图片信息 存储图片:bitmap private byte[] getIconData(Bitmap bitmap){ int size = bitmap.get ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...
- Android——SQLite/数据库 相关知识总结贴
android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...
- Android Sqlite数据库加密
Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个: 1. 对几个关键的字段使用加密算法,再存入数据库 2. 对整个数据库进行加密 SQLite数据库加密工具: 收 ...
- Android SQLite数据库使用
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...
- android: SQLite 数据库的最佳实践
6.5.1 使用事务 前面我们已经知道,SQLite 数据库是支持事务的,事务的特性可以保证让某一系列的操 作要么全部完成,要么一个都不会完成.那么在什么情况下才需要使用事务呢?想象以下场 景, ...
- Android—SQLITE数据库的设计和升降级
Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...
随机推荐
- 使用compile_scripts.php脚本,生成lua打包的zip,解决加密问题
@echo off set DIR=%~dp0 set TEMPLATE_ROOT=%DIR%.. echo %TEMPLATE_ROOT%\quick\bin\win32\php.exe echo ...
- rtc关机闹钟4 AlarmManagerService.java
vim base/services/core/java/com/android/server/AlarmManagerService.java AlarmManager 调用 IAlarmManage ...
- AGPS 常见的两种定位模式
SI 定位模式: 用户发起定位请求,辅助GPS 模块快速进行定位.时间在6秒-15秒之间. 这个方式能够有效的解决普通GPS 最快需要30秒时间获得卫星星历的搜星慢的问题,如果使用AGPS将通过中移动 ...
- C# PDF添加水印
需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/ using System;using System.Collecti ...
- CSUFT 1005 Coffin Tiles
1005: Coffin Tiles Time Limit: 1 Sec Memory Limit: 128 MB Submit: 2 Solved: 2 Description ...
- js九九乘法表
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- SharePoint API测试系列——Records.BypassLocks测试
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 对于SharePoint中已经是Record的Item,我们想要修改他的属性,这在UI界面是无法完 ...
- randperm函数
randperm功能是随机打乱一个数字序列. 语法格式: y = torch.randperm(n) y是把1到n这些数随机打乱得到的一个数字序列. th> torch.randperm() [ ...
- QT笔记之实现阴影窗口
方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...
- SqlSever基础 两个条件 group by 分组显示
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...