- package com.xiaoshan.udp.client.db;
- import Android.content.ContentValues;
- import android.database.Cursor;
- import android.test.AndroidTestCase;
- /**
- * 单元测试操作sqlLite的各种sql
- *
- * @author 单红宇
- */
- public class TestSqlLite extends AndroidTestCase {
- /**
- * 创建表
- *
- * @throws Exception
- */
- public void createTable() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- String deleteSql = "drop table if exists user ";
- dbHelper.execSQL(deleteSql);
- // id是自动增长的主键,username和 password为字段名, text为字段的类型
- String sql = "CREATE TABLE user (id integer primary key autoincrement, username text, password text)";
- dbHelper.execSQL(sql);
- dbHelper.closeConnection();
- }
- /**
- * 插入数据
- *
- * @throws Exception
- */
- public void insert() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- ContentValues values = new ContentValues(); // 相当于map
- values.put("username", "test");
- values.put("password", "123456");
- dbHelper.insert("user", values);
- dbHelper.closeConnection();
- }
- /**
- * 更新数据
- *
- * @throws Exception
- */
- public void update() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- ContentValues initialValues = new ContentValues();
- initialValues.put("username", "changename"); // 更新的字段和值
- // 第三个参数为条件语句
- dbHelper.update("user", initialValues, "id = ?", new String[] { "1" });
- dbHelper.closeConnection();
- }
- /**
- * 删除数据
- *
- * @throws Exception
- */
- public void delete() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- dbHelper.delete("user", "id =?'", new String[] { "1" });
- dbHelper.closeConnection();
- }
- /**
- * 增加字段
- *
- * @throws Exception
- */
- public void addColumn() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- String updateSql = "alter table user add company text";
- dbHelper.execSQL(updateSql);
- dbHelper.closeConnection();
- }
- /**
- * 查询列表
- *
- * @throws Exception
- */
- public void selectList() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- Cursor returnCursor = dbHelper.findList(false, "user", new String[] { "id", "username", "password" }, "username?", new String[] { "test" }, null, null, "id desc", null);
- while (returnCursor.moveToNext()) {
- String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
- String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
- String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
- System.out.println("id=" + id + ";username=" + username + ";" + password + ";\n");
- }
- dbHelper.closeConnection();
- }
- /**
- * 某一条信息
- *
- * @throws Exception
- */
- public void selectInfo() throws Exception {
- DBHelper dbHelper = new DBHelper(this.getContext());
- dbHelper.open();
- Cursor returnCursor = dbHelper.findOne(false,"user", new String[] { "id", "username", "password" }, "id = '1'", null, null, null, "id desc",null);
- if (returnCursor != null) {
- String id = returnCursor.getString(returnCursor.getColumnIndexOrThrow("id"));
- String username = returnCursor.getString(returnCursor.getColumnIndexOrThrow("username"));
- String password = returnCursor.getString(returnCursor.getColumnIndexOrThrow("password"));
- System.out.println("id=" + id + ";username=" + username + ";" + password + ";\n");
- }
- }
- }
如上代码和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
