SQLiteDatabase的方式会生成一个数据库文件,每个应用最多只对应一个数据库文件,即.db文件。

可以使用很多第三方工具进行打开,查看数据库里的内容。

昨晚试了好几种工具,如navicat,sqlite3,sqlitespy,等,还是觉得sqlitespy比较好用,而且体积也小,才1.7M。文件管理里有对应的sqlitespy。省得以后到处找sqlite的打开工具。

  前面的SharedPreferences的存储方式就类似于web应用中的表单的存储。

而SQLiteDatabase类似于把数据存储到Oracle中,只是安卓用是保存到sqlite数据库中而已。

注意点:

sqlite建表sql和Oracle中不同的地方:

Oracle中:

create table usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )

  或者

create or replace table usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )

sqlite中:

create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )

  一定要加if not exists这个判断,sqlite中已经有了需要建的表,而建表的时候不加if not exists就会报错,因为不能够重复建同样表名的表。

MainActivity.java如下:

package com.example.sqlitedatabasetest;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 每个程序都有自己的数据库 默认情况下是各自互相不干扰
// 创建一个数据库 并且打开
// SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
// db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )");
//// db.execSQL("create table usertb2 (_id integer primary key, name text not null , age integer not null , sex text not null )");
//
// System.out.println("===================>");
// db.execSQL("insert into usertb(name,sex,age) values('张三','女',18)");
// db.execSQL("insert into usertb(name,sex,age) values('李四','女',19)");
// db.execSQL("insert into usertb(name,sex,age) values('王五','男',20)");
// System.out.println("!!!!!!!!!88888888234!!!!!!!!");
// Cursor c = db.rawQuery("select * from usertb", null);
// if (c != null) {
// while (c.moveToNext()) {
// Log.i("info", "_id:" + c.getInt(c.getColumnIndex("_id")));
// Log.i("info", "name:" + c.getString(c.getColumnIndex("name")));
// Log.i("info", "age:" + c.getInt(c.getColumnIndex("age")));
// Log.i("info", "sex:" + c.getString(c.getColumnIndex("sex")));
// Log.i("info", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// }
// c.close();
// }
// db.close(); //创建database
SQLiteDatabase db = openOrCreateDatabase("wyl.db", MODE_PRIVATE, null);
db.execSQL("create talbe if not exists wyl_table "
+ " (_id integer primary key autoincrement,"
+ "name text not null,sex text not null");
//
/**
* create table if not exists usertb (_id integer primary key
* autoincrement, name text not null , age integer not null , sex text not null )
*/
db.execSQL("insert into wyl_table (name,sex) values ('wyl','男')");
db.close();
} }

  

最新文章

  1. MyEclipse中折叠和展开所有代码
  2. 20150618_Andriod _KSOAP2_多线程
  3. 如何学好oracle?(准备)
  4. Ios下解决libzbar.a不支持armv7s的方法
  5. 使用IIS配合VS调试
  6. clipboard让复制的文本换行
  7. Codevs 3304 水果姐逛水果街Ⅰ 线段树
  8. 把 图片 资源文件 编译到dll
  9. linux串口驱动分析——发送数据
  10. VirtualBox镜像复制载入
  11. C#--遍历目录实例
  12. select count(*) 底层究竟做了什么?
  13. VULKAN学习资料收集
  14. Mac下的Docker及Kubernetes(k8s)本地环境搭建与应用部署、管理界面kubernetes-dashboard
  15. Andorid 翻书效果
  16. 细说C#继承
  17. Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)
  18. AtCoder Grand Contest 030 Solution
  19. 离线服务器下docker的部署与应用
  20. 自己动手做AI:Google AIY开发工具包解析

热门文章

  1. 5.6.1 Boolean类型
  2. iOS 支持arm_64 和 x86_64 的OpenSSL 静态库(libcrypto.a, libssl.a)
  3. python defaultdict 类型
  4. Filter 知识总结
  5. jQuery EasyUI 数字框(NumberBox)用法
  6. CCPC L(水)
  7. C#实现的内存分页机制的一个实例
  8. PHP移动互联网开发(1)——环境搭建及配置
  9. Sql语句之select 5种查询
  10. [译]MDX 介绍