Android数据库相关代码解读(1)
来源:互联网 发布:xd苹果软件 编辑:程序博客网 时间:2024/05/20 05:06
Android数据库的操作方法多样化,掌握这些应用技巧对于我们的实际开发起着非常重要的作用。我们在这里就为大家介绍了一下相关操作技巧。
AD: 51CTO云计算架构师峰会 抢票进行中!
在Android 手机操作系统进行实际开发中,进场会应用到数据库。而且在这一平台中对数据库的应用方法比较简单灵活。我们在这里就为大家详细介绍了相关方法,希望可以给大家带来一些帮助。
昨天进行了GUI界面设计,感受了一下android初次设计的愉悦,今天接着学习其SQLite数据库试用,将昨天的例子中数据存到数库中,并读取查看一下。 具体看代码(原写的有点问题,再改写如下):
1) Android数据库之库操作类:
- package com.topsun;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class DBHelper {
- private static final String TAG = "UserDB_DBHelper.java";
- private static final String DataBaseName = "UserDB";
- SQLiteDatabase db;
- Context context;
- public DBHelper(Context context) {
- this.open(context);
- }
- private void createTabel() {
- // TODO Auto-generated method stub
- String sql = "";
- try {
- sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER
PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)";- this.db.execSQL(sql);
- Log.v(TAG, "Create Table TestUser ok");
- } catch (Exception e) {
- Log.v(TAG, "Create Table TestUser fail");
- } finally {
- //this.db.close();
- Log.v(TAG, "Create Table TestUser ");
- }
- }
- public boolean save(String name, String sex, Integer ages) {
- String sql = "insert into TestUser values
(null,'" + name + "','" + sex- + "'," + ages + ")";
- try {
- this.db.execSQL(sql);
- Log.v(TAG, "insert Table TestUser 1 record ok");
- return true;
- } catch (Exception e) {
- Log.v(TAG, "insert Table TestUser 1 record fail");
- return false;
- } finally {
- //this.db.close();
- Log.v(TAG, "insert Table TestUser ");
- }
- }
- public Cursor loadAll() {
- Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null,- null, null, null, null);
- return cur;
- }
- public void open(Context context){
- if (null == db || !this.db.isOpen()){
- this.context = context;
- this.db = context.openOrCreateDatabase(this.DataBaseName,
- context.MODE_PRIVATE, null);
- createTabel();
- Log.v(this.TAG, "create or Open DataBase。。。");
- }
- }
- public void close() {
- db.close();
- }
- }
- package com.topsun;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class DBHelper {
- private static final String TAG = "UserDB_DBHelper.java";
- private static final String DataBaseName = "UserDB";
- SQLiteDatabase db;
- Context context;
- public DBHelper(Context context) {
- this.open(context);
- }
- private void createTabel() {
- // TODO Auto-generated method stub
- String sql = "";
- try {
- sql = "CREATE TABLE IF NOT EXISTS TestUser
(ID INTEGER PRIMARY KEY autoincrement,
NAME TEXT, SEX TEXT, AGES INTEGER)";- this.db.execSQL(sql);
- Log.v(TAG, "Create Table TestUser ok");
- } catch (Exception e) {
- Log.v(TAG, "Create Table TestUser fail");
- } finally {
- //this.db.close();
- Log.v(TAG, "Create Table TestUser ");
- }
- }
- public boolean save(String name, String sex, Integer ages) {
- String sql = "insert into TestUser values
(null,'" + name + "','" + sex- + "'," + ages + ")";
- try {
- this.db.execSQL(sql);
- Log.v(TAG, "insert Table TestUser 1 record ok");
- return true;
- } catch (Exception e) {
- Log.v(TAG, "insert Table TestUser 1 record fail");
- return false;
- } finally {
- //this.db.close();
- Log.v(TAG, "insert Table TestUser ");
- }
- }
- public Cursor loadAll() {
- Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null,- null, null, null, null);
- return cur;
- }
- public void open(Context context){
- if (null == db || !this.db.isOpen()){
- this.context = context;
- this.db = context.openOrCreateDatabase(this.DataBaseName,
- context.MODE_PRIVATE, null);
- createTabel();
- Log.v(this.TAG, "create or Open DataBase。。。");
- }
- }
- public void close() {
- db.close();
- }
- }
- Android数据库相关代码解读(1)
- Android--数据库相关代码解读
- Android震动代码解读
- android 反编译和代码解读
- android 反编译和代码解读
- android 反编译和代码解读
- android 反编译和代码解读
- android 反编译和代码解读
- android典型代码系列(二十九)------短信数据库操作相关
- Android数据库代码优化(1)
- android 多媒体相关代码
- android 多媒体相关代码
- Android init相关代码
- android代码混淆相关
- Android代码混淆相关
- Android录音相关代码
- android 数据库相关操作
- android文件数据库相关
- 重定向输出结果
- 使用git clone出现SSL routines:SSL3_GET_SERVER_CERTIFICATE错误的一种解决办法
- 从页面上导出数据到Excel中
- QTP_QTP11过30天试用期如何恢复 [quickTest Professional]
- hdu 3535 涵盖了分组背包的各种情况 非常好的背包题目
- Android数据库相关代码解读(1)
- Linux中Apache+Tomcat+JK实现负载均衡和群集的完整过程(转载)
- svn 获取之前版本
- Javascript触发Ajax回传,并且触发特定的事件
- UI 的旋转
- C# 事件机制
- mediawiki(LM智库)搭建手册
- WinForm 窗口最大化(覆盖任务栏)
- java中进制之间的转换