在Android中存储数据可以用文件、数据库、网络,其中文件和数据库是最常用的,数据库我们常用的就是Sqlite,它是一种经量级的、嵌入式的关系型数据库;在android中当需要操作SQLite数据库的时候需要得到一个SQLiteOpenHelper对象,而SQLiteOpenHelper是一个抽象类,用户需要继承这个类

package com.example.sqlite.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; /**
* DatabaseHelper作为一个访问SQLite的助手类,提供两个方面的功能,
* 第一,getReadableDatabase(),getWritableDatabase()可以获得SQLiteDatabse对象,通过该对象可以对数据库进行操作
* 第二,提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作
* @author Administrator
*
*/
public class DatabaseHelper extends SQLiteOpenHelper{ public static final int VERSION = 1;
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public DatabaseHelper(Context context, String name){
this(context, name, VERSION);
Log.i("debug", "DatabaseHelper");
}
public DatabaseHelper(Context context, String name, int version){
this(context, name, null, version);
} @Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i("debug", "create a database");
//执行SQL语句
db.execSQL("create table user (id int, name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i("debug", "Upgrade a database");
}
}

下面是对数据库的进行增、删、改、查操作

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/create_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/create_db"/>
<Button
android:id="@+id/update_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/update_db"/>
<Button
android:id="@+id/insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/insert"/>
<Button
android:id="@+id/update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/update"/>
<Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/query"/>
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/> </LinearLayout>

在Activity处理代码

package com.example.sqlite;

import com.example.sqlite.db.DatabaseHelper;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; /**
* SQLite的创建、更新、修改、插入、查询、删除操作
* @author Administrator
*
*/
public class SqliteActivity extends Activity { private Button create_db;
private Button update_db; //更新数据库的版本号
private Button insert;
private Button update;
private Button query;
private Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
create_db = (Button)findViewById(R.id.create_db);
update_db = (Button)findViewById(R.id.update_db);
insert = (Button)findViewById(R.id.insert);
update = (Button)findViewById(R.id.update);
query = (Button)findViewById(R.id.query);
delete = (Button)findViewById(R.id.delete);
create_db.setOnClickListener(new CreateListener());
update_db.setOnClickListener(new UpdateDBListener());
insert.setOnClickListener(new InsertListener());
update.setOnClickListener(new UpdateListener());
query.setOnClickListener(new QueryListener());
delete.setOnClickListener(new Deletelistener());
} class CreateListener implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("debug", "on create db");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db");
//只有调用了DatabaseHelper的getReadableDatabase()或getWritableDatabase()才会创建数据库
SQLiteDatabase db = dbHelper.getWritableDatabase();
} }
/**
* 更新数据库的版本号
* @author Administrator
*
*/
class UpdateDBListener implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("debug", "on update db");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db", 2);
SQLiteDatabase db = dbHelper.getReadableDatabase();
} } class InsertListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("debug", "insert db");
ContentValues values = new ContentValues();
//向该对象插入键值对,其中键是列名,值是希望插入这一列的值,值的数据类型必须一致
values.put("id", 1);
values.put("name", "zhang");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db");
SQLiteDatabase db = dbHelper.getWritableDatabase();
//调用insert方法插入数据
db.insert("user", null, values);
} }
class UpdateListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("debug", "update db");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db");
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "zhangsanfeng");
//第一个参数是要更新的表名
//第二个参数是一个ContentValeus对象
//第三个参数是where子句
db.update("user", values, "id=?", new String[]{"1"});
} }
class QueryListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("debug", "query db");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
int count = cursor.getCount();
System.out.println("query---count = >" + count);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println("query--->" + name);
}
} }
class Deletelistener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("debug", "delete db");
DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db");
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("user", "id=?", new String[]{"1"});
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_sqlite, menu);
return true;
} }

运行效果

样例代码

点击打开链接

Android开发之Sqlite的使用的更多相关文章

  1. Android开发之SQLite的使用方法

    前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...

  2. [转]ANDROID开发之SQLite详解

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...

  3. ANDROID 开发之 SQLite

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...

  4. ANDROID开发之SQLite详解

    本文转自:http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html

  5. Android开发之旅4:应用程序基础及组件

    引言 为了后面的例子做准备,本篇及接下来几篇将介绍Android应用程序的原理及术语,这些也是作为一个Android的开发人员必须要了解,且深刻理解的东西.本篇的主题如下: 1.应用程序基础 2.应用 ...

  6. Android开发之旅3:android架构

    引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWorld Android 开发之旅:HelloWorld项目的目录结构 我们对android有了个大致的了解,知道如何搭建andr ...

  7. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

  8. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  9. Android开发之Git配置

    Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...

随机推荐

  1. container_of用法及实现

    container_of 有的情况下,只知道 struct结构中莫个成员的指针,而需要知道整个struct的指针 (如网卡驱动里面,list) struct DDD {         int a; ...

  2. PHP - 接口 - 单一接口

    /* * 接口的使用 */ //定义接口 interface IPerosn{ public function eat(); public function water(); } //定义继承自接口的 ...

  3. Table中的JCheckBox TableHeader的全选(全反选)功能

    菜鸟学习ing class CheckDefaultModel extends DefaultTableModel /* * To change this template, choose Tools ...

  4. 基于visual Studio2013解决C语言竞赛题之1022最大数最小数

         题目 解决代码及点评 /************************************************************************/ ...

  5. C# c++ 传递函数指针

    C#和c++之间相互传递函数指针 在C++和C#之中都有很多callback method,可以相互调用吗,怎么传递,是我表弟的问题. 1.定义c++ dll ,导出方法 // sort.cpp : ...

  6. ZJUT 1423 地下迷宫(期望DP&高斯消元)

    地下迷宫 Time Limit:1000MS  Memory Limit:32768K Description: 由于山体滑坡,DK被困在了地下蜘蛛王国迷宫.为了抢在DH之前来到TFT,DK必须尽快走 ...

  7. getAttribute()获取属性

    Js:getAttribute[转] 一份文档就是一棵节点树. ●节点分为不同的类型:元素节点.属性节点和文本节点等. ●getElementById()方法将返回一个对象,该对象对应着文档里的一个特 ...

  8. Python前世今生

    Python前世今生   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时 ...

  9. Delphi + Asm - TBits类的学习

    技术交流,DH讲解. 在D2010的classes中有个TBits类,这个类主要是位操作的. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 TBits = class privat ...

  10. Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading data from binary lo

    mysql> show slave status\G *************************** 1. row ***************************         ...