Android存储之SQLiteDatbase
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();
} }
Android存储之SQLiteDatbase的更多相关文章
- Android存储访问及目录
Android存储访问及目录 Android的外部存储 Android支持外部存储(case-insensitive filesystem with immutable POSIX permissio ...
- Android存储之SQLite数据库
Android存储之SQLite数据库数据库 创建数据库 package --; import android.content.Context; import android.database.sql ...
- Android存储路径你了解多少?
在了解存储路径之前,先来看看Android QQ的文件管理界面,了解一下QQ的数据文件路径来源,到底是来源于什么地方? 手Q文件管理对应存储目录 我的文件:是指放在QQ指定目录下的文件:/tencen ...
- Android 存储(本地存储 SD卡存储 SharedPreference SQLite ContentProvider)
本文出自:http://blog.csdn.net/dt235201314/article/details/73176149 源码下载欢迎Star(updating):https://github.c ...
- 一篇文章搞懂android存储目录结构
前言 前两天因为开发一个app更新的功能,我将从服务器下载的apk文件放在了内部存储目录(测试手机为小米,路径为:data/user/0/packagename/files)下面,然后安装的时候一直安 ...
- android存储方式的应用场景
作为一个完整的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.文件存储.SQLite. Content Provider ...
- Android开发学习——android存储
Android的存储 内部存储空间RAM内存:运行内存,相当于电脑的内存ROM内存:存储内存,相当于电脑的硬盘外部存储空间 SD卡:相当于电脑的移动硬盘 * 2.2之前,sd卡路径:sdcard ...
- Android存储
Android的四种数据存储方式: 1.SharedPrefrences 2.SQLite 3.Content Provider 4.File SharedPrefrences: 1.是一种轻型的数据 ...
- 利用反射得到android存储路径
获得android手机的存储路径: public String getPrimaryStoragePath(){ try{ StorageManager sm = (StorageManager) c ...
随机推荐
- ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值
通过jQuery来获取RadioButtonList成员内容. <%@ Page Language="C#" AutoEventWireup="true" ...
- Matplotlib中文乱码
想要分析一批数据,画出图形会比较直观.所以就搜索了一下各种软件,最终选择使用python的matplotlib.原因也是因为python使用起来比较方便,虽然R才是分析数据的首选,不过,没有R的基础, ...
- Correlation rule tuning
Lots of organizations are deploying SIEM systems either to do their due diligence or because it’s pa ...
- c++builder 重载WindowProc、WndProc 截获消息(比Delphi多一个Message Map方法)
c++builder 重载WindowProc.WndProc 截获消息 方法一WindowProc void __fastcall myWindowProc(Messages::TMessage ...
- vs linq to db template
linq to db template 支持sqlite. mysql .db2. accress. oracle. Firebird等多种数据库以linq方式操作数据. NuGet NuGet 是 ...
- HA for openstack
mysql ha instance ha openstack博客:http://blog.csdn.net/tantexian/article/list/2 使用eclipse远程调试openstac ...
- STL之string
string构造函数,很多重载函数: #include <iostream> #include <string> using namespace std; int main() ...
- Ext JS学习第十五天 Ext基础之 Ext.DomQuery
此文同来记录学习笔记 •Ext.dom.Query 嗯,这个类一看就是到时做什么事儿的,不用我去过多的解释了.这个类一共提供了8个方法供开发人员去使用. •要说最常用的方法,无非就是Ext.query ...
- [爬虫]通过url获取连接地址中的数据
1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用using HtmlAgilityPack; 2. 详细步骤如下: 步骤一 ...
- 关闭Outlook的时候使之最小化
Outlook很搓的一点就是只有按‘最小化’按钮的时候才会最小化到托盘,而按‘关闭’按钮Outlook直接被关闭退出.然后经常发现没邮件,结果是因为客户端关掉了. 下面通过插件方式实现关闭后最小化到托 ...