手机版
你好,游客 登录 注册
背景:
阅读新闻

Android数据库(SqlLite)操作和db文件查看

[日期:2012-05-22] 来源:Linux社区  作者:catoop [字体:大 中 小]
测试类的代码:
  1. package com.xiaoshan.udp.client.db;  
  2.   
  3. import Android.content.ContentValues;  
  4. import android.database.Cursor;  
  5. import android.test.AndroidTestCase;  
  6.   
  7.   
  8. /** 
  9.  * 单元测试操作sqlLite的各种sql 
  10.  *  
  11.  * @author 单红宇 
  12.  */  
  13. public class TestSqlLite extends AndroidTestCase {  
  14.   
  15.     /** 
  16.      * 创建表 
  17.      *  
  18.      * @throws Exception 
  19.      */  
  20.     public void createTable() throws Exception {  
  21.         DBHelper dbHelper = new DBHelper(this.getContext());  
  22.         dbHelper.open();  
  23.   
  24.         String deleteSql = "drop table if exists user ";  
  25.         dbHelper.execSQL(deleteSql);  
  26.   
  27.         // id是自动增长的主键,username和 password为字段名, text为字段的类型   
  28.         String sql = "CREATE TABLE user (id integer primary key autoincrement, username text, password text)";  
  29.         dbHelper.execSQL(sql);  
  30.         dbHelper.closeConnection();  
  31.     }  
  32.   
  33.     /** 
  34.      * 插入数据 
  35.      *  
  36.      * @throws Exception 
  37.      */  
  38.     public void insert() throws Exception {  
  39.         DBHelper dbHelper = new DBHelper(this.getContext());  
  40.         dbHelper.open();  
  41.   
  42.         ContentValues values = new ContentValues(); // 相当于map   
  43.   
  44.         values.put("username", "test");  
  45.         values.put("password", "123456");  
  46.   
  47.         dbHelper.insert("user", values);  
  48.   
  49.         dbHelper.closeConnection();  
  50.     }  
  51.   
  52.     /** 
  53.      * 更新数据 
  54.      *  
  55.      * @throws Exception 
  56.      */  
  57.     public void update() throws Exception {  
  58.         DBHelper dbHelper = new DBHelper(this.getContext());  
  59.         dbHelper.open();  
  60.         ContentValues initialValues = new ContentValues();  
  61.         initialValues.put("username", "changename"); // 更新的字段和值   
  62.         // 第三个参数为条件语句   
  63.         dbHelper.update("user", initialValues, "id = ?", new String[] { "1" });  
  64.   
  65.         dbHelper.closeConnection();  
  66.     }  
  67.   
  68.     /** 
  69.      * 删除数据 
  70.      *  
  71.      * @throws Exception 
  72.      */  
  73.     public void delete() throws Exception {  
  74.         DBHelper dbHelper = new DBHelper(this.getContext());  
  75.         dbHelper.open();  
  76.   
  77.         dbHelper.delete("user", "id =?'", new String[] { "1" });  
  78.   
  79.         dbHelper.closeConnection();  
  80.     }  
  81.   
  82.     /** 
  83.      * 增加字段 
  84.      *  
  85.      * @throws Exception 
  86.      */  
  87.     public void addColumn() throws Exception {  
  88.         DBHelper dbHelper = new DBHelper(this.getContext());  
  89.         dbHelper.open();  
  90.   
  91.         String updateSql = "alter table user add company text";  
  92.         dbHelper.execSQL(updateSql);  
  93.           
  94.         dbHelper.closeConnection();  
  95.     }  
  96.   
  97.     /** 
  98.      * 查询列表 
  99.      *  
  100.      * @throws Exception 
  101.      */  
  102.     public void selectList() throws Exception {  
  103.         DBHelper dbHelper = new DBHelper(this.getContext());  
  104.         dbHelper.open();  
  105.           
  106.         Cursor returnCursor = dbHelper.findList(false, "user", new String[] { "id", "username", "password" }, "username?", new String[] { "test" }, null, null, "id desc", null);  
  107.         while (returnCursor.moveToNext()) {  
  108.             String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));  
  109.             String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));  
  110.             String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));  
  111.             System.out.println("id=" + id + ";username=" + username + ";" + password + ";\n");  
  112.         }  
  113.           
  114.         dbHelper.closeConnection();  
  115.     }  
  116.   
  117.     /** 
  118.      * 某一条信息 
  119.      *  
  120.      * @throws Exception 
  121.      */  
  122.     public void selectInfo() throws Exception {  
  123.         DBHelper dbHelper = new DBHelper(this.getContext());  
  124.         dbHelper.open();  
  125.         Cursor returnCursor = dbHelper.findOne(false,"user", new String[] { "id", "username", "password" }, "id = '1'", null, null, null, "id desc",null);  
  126.         if (returnCursor != null) {  
  127.             String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));  
  128.             String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));  
  129.             String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));  
  130.             System.out.println("id=" + id + ";username=" + username + ";" + password + ";\n");  
  131.         }  
  132.     }  
  133. }  
另外,sqllite数据库的db文件可以直接使用工具查看,具体工具是SQLiteSpy

如上代码和SQLiteSpy.exe查看下载地址为:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/5月/22日/Android数据库(SqlLite)操作和db文件查看/

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android编程入门 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* android小菜鸟 发表于 2012/9/11 14:56:39
很好用
谢谢楼主