android——数据存储SQLite
来源:互联网 发布:数据作用 编辑:程序博客网 时间:2024/05/16 02:12
SQLite是一种轻量级的关系型数据库,它的运算速度非常的快,占用资源很少,特别适合在移动设备上使用
创建数据库
下面我们创建一个名为Book和Category的数据库
建立MydatabaseHelper类继承自SQLiteOpenHelper类代码如下:
public class MyDatabaseHelper extends SQLiteOpenHelper {public static final String CREATE_BOOK="create table Book (" + "id integer primary key autoincrement," + "author text," + "price real," + "pages integer," + "name text)";public static final String CREATE_CATEGORY="create table Category(id integer primary key autoincrement," + "category_name text," + "category_code integer)";private Context mContext;public MyDatabaseHelper(Context context, String name,CursorFactory factory, int version) {super(context, name, factory, version);// TODO Auto-generated constructor stubmContext=context;}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL(CREATE_BOOK);db.execSQL(CREATE_CATEGORY);Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_LONG).show();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubdb.execSQL("drop table if exists Book");db.execSQL("drop table if exists Category");onCreate(db);}}
SQLiteOpenHelper中有二个非常重要的实例方法 ,getReadableDatabase()和getWritableDatabase()。这二个方法都可以创建或打开一个现有的数据库文件,并返回一个可对数据库进行读写操作的对象。不同的是当数据库不可写入的时候(如磁盘空间已满)getReadableDatabase()方法返回的对象将以只读的方式打开数据库,而getWritableDatabase()方法将出现异常。
SQLiteOpenHelper中二个抽象方法,分别是onCreate()和onUpgrade(),我们必须在自己的帮助类中重写这二个方法,然后分别在这二个方法中去实现创建,升级数据库的逻辑。
SQLiteOpenHelper中有二个构造方法可供重写,一般使用参数较少的那个构造方法,这个方法接收四个参数,第一个参数是Context,必须有它才可以对数据库进行操作。第二个参数是数据库名,创建数据库时使用的就是这里指定的名称。第三个参数允许我们在查询数据的时候返回一个自定义的Cursor,一般传入null。第四个参数表示当前的数据库版本号,用于数据库的升级升级操作。
调用getReadableDatabase()或getWritableDatabase()方法就能够创建数据库了。
数据库创建成功后数据库文件存放在/data/dat/<package name>/databases/目录下。
创建activity_main.xml布局
代码如下所示:
<Button android:id="@+id/create_database" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="create database" /> <Button android:id="@+id/addButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Add data" /> <Button android:id="@+id/UpdateButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Update data" /> <Button android:id="@+id/DeletButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Delete data" /> <Button android:id="@+id/QueryButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Query data" />
在MainActivity中实现对数据库的增,删,改,查功能
代码如下:
public class MainActivity extends Activity {private MyDatabaseHelper dbHelper;private Button createDatabase;private Button adddata;private Button updatedata;private Button deletedata;private Button querydata;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);createDatabase=(Button)findViewById(R.id.create_database);adddata=(Button)findViewById(R.id.addButton);updatedata=(Button)findViewById(R.id.UpdateButton);deletedata=(Button)findViewById(R.id.DeletButton);querydata=(Button)findViewById(R.id.QueryButton);createDatabase.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubdbHelper.getWritableDatabase();}});adddata.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name", "The Da Vinci Code");values.put("author", "Dan Brown");values.put("pages", 454);values.put("price", 16.96);db.insert("Book", null, values);values.put("name", "The Lost Symbol");values.put("author", "Dan Brown");values.put("pages", 510);values.put("price", 19.95);db.insert("Book", null, values);}});updatedata.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("price", 10.99);db.update("Book", values, "name = ?", new String[] {"The Da Vinci Code"});}});deletedata.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString whereClause="id = ?";String[] whereArgs={"11"};SQLiteDatabase db=dbHelper.getWritableDatabase();//db.delete("Book", "id = ?", new String[] {"11"});db.delete("Book", whereClause, whereArgs);}});querydata.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSQLiteDatabase db =dbHelper.getWritableDatabase();Cursor cursor=db.query("Book", null, null, null, null, null, null);if(cursor.moveToFirst()){do{String name = cursor.getString(cursor.getColumnIndex("name"));String author = cursor.getString(cursor.getColumnIndex("author"));int pages = cursor.getInt(cursor.getColumnIndex("pages"));double price = cursor.getDouble(cursor.getColumnIndex("price"));Log.d("MainActivity", "book name is "+name);Log.d("MainActivity", "book author is "+author);Log.d("MainActivity", "book pages is "+pages);Log.d("MainActivity", "book price is "+price);}while(cursor.moveToNext());}cursor.close();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}通过以上代码就可以对数据库有一定的了解了。
- android数据存储—SQLite
- Android数据存储——SQLite
- android——数据存储SQLite
- Android——数据库sqlite存储数据
- Android数据存储方式—SQLite
- Android数据存储—使用SQLite数据库
- Android数据存储—使用SQLite数据库
- Android中数据存储——SQLite数据库存储数据
- Android数据存储——SQLite数据库存储
- android数据存储--SQLite
- Android数据存储---SQLite
- Android数据存储-SQLite
- android-数据存储sqlite
- Android---数据存储SQLite
- Android 数据存储----SQLite
- android SQLite 数据存储
- Android SQLite数据存储
- 数据存储——SQLite
- 腾讯某论坛存在SSRF漏洞(附批量捡漏脚本) ------ 20160614
- Linux软件管理器(如何使用软件管理器来管理软件)
- linux 版本
- Android Scroll分析(二)——滑动的几种方法
- 常用linux 命令总结
- android——数据存储SQLite
- Qt安卓JNI交互之(1) C++注册函数给JAVA调用
- DOCKER windows安装
- LeetCode OJ 9. Palindrome Number
- 关于App内跳转到设置界面的问题
- python学习笔记:ipython,scipy,pandas,flask
- 最好的学习时光是四年前,其次是现在
- Oracle 11g R2在linux上安装前配置(不适用于ubuntu)
- [源码分析][Linux]内核工具Sparse