SQLite CRUD操作代码实例:

1:首先创建一个继承了SQLiteOpenHelper类的MyDatabaseHelper类。实现他的onCreate(SQLiteDatabase db)

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法。

package dataBase.databasetest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context mContext; public MyDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
} public static final String CREATE_BOOK="create table book(" //创建book表
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
public static final String CREATE_CATEGORY="create table category(" //创建category表
+"id integer primary key autoincrement,"
+"category_name text,"
+"category_code integer)";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO 自动生成的方法存根
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext, "创建数据库成功", Toast.LENGTH_LONG).show();
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO 自动生成的方法存根
db.execSQL("drop table if exists book"); //如果已有这两个表。先删除,
db.execSQL("drop table if exists category"); //在调用onCreate()分别创建
onCreate(db);
} }

2.在MainActivity中通过几个按钮事件测试对数据的CRUD:

 package dataBase.databasetest;

 import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//修改参数为2,执行onUpgrade()方法跟新数据库
dbHelper=new MyDatabaseHelper(this, "BookStor.db", null, 2); //实现构造函数,传入参数,第一个为context。第二个为数据库名称
//创建表
Button create=(Button)this.findViewById(R.id.creat);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dbHelper.getWritableDatabase();
}
}); //增加数据
Button add=(Button)this.findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//开始组装数据
values.put("name", "Android第一行代码");
values.put("author", "郭霖");
values.put("price",66.78);
values.put("pages",400 );
//写入数据
db.insert("book", null, values);
values.clear();
//准备再次写入数据
values.put("name","java讲义");
values.put("author", "张三");
values.put("price", 66.76);
values.put("pages", 789);
db.insert("book", null, values);
values.clear();
Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_LONG).show();
}
}); //更新数据
Button updata=(Button)this.findViewById(R.id.updata);
updata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price", 100.00);
db.update("book", values, "name=?",new String[]{"平凡的世界"});
values.clear();
Toast.makeText(getApplicationContext(), "数据更新成功", Toast.LENGTH_SHORT).show(); }
}); //删除数据
Button delete =(Button)this.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("book", "id>?", new String[]{"23"}); //第一个参数为表名,二三个参数为限制条件
Toast.makeText(getApplicationContext(), "数据删除成功", Toast.LENGTH_SHORT).show();
}
}); //查询数据
Button find =(Button)this.findViewById(R.id.find);
find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase 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", name);
Log.d("MainActivity", author);
Log.d("MainActivity", String.valueOf(pages));
Log.d("MainActivity", String.valueOf(price));
} while (cursor.moveToNext());
}
cursor.close();
Toast.makeText(getApplicationContext(), "数据查找完毕",Toast.LENGTH_SHORT).show();
}
}); //事物
Button transAction=(Button)this.findViewById(R.id.transaction);
transAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.beginTransaction();//开启事务
try {
ContentValues values=new ContentValues();
db.delete("book", "id=?", new String[]{"11"}); //删除
values.put("name", "newnewnew");
values.put("price",111);
values.put("author", "牛人");
values.put("pages", 222);
db.update("book", values, "name=?", new String[]{"Android第一行代码"});
Toast.makeText(getApplicationContext(), "事务执行完毕", Toast.LENGTH_SHORT).show();
values.clear();
} catch (Exception e) {
// TODO: handle exception
}finally{
db.endTransaction();//关闭事务
}
}
});
}
}

3:查看数据库数据可以通过在dos中运行 adb shell 或者 SQLite Expert软件查看。

SQLite CRUD操作的更多相关文章

  1. SQLite数据库操作 (原始操作)

    android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API, 使用该类可以完成对数据进行添加(Create).查询(Retrieve).更新(Update)和删除( ...

  2. ORM对象关系映射之使用GreenDAO进行CRUD操作

    在Android中,我们都知道使用的数据库是SQLite,而使用这种原生的数据库非常繁琐,它对表的管理和进行CRUD操作都需要我们写sql语句,在进行多表关联的操作上,更是需要写一堆sql,而且维护起 ...

  3. 【翻译】MongoDB指南/CRUD操作(四)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...

  4. 【翻译】MongoDB指南/CRUD操作(三)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(三) 主要内容: 原子性和事务(Atomicity and Transactions),读隔离.一致性和新近 ...

  5. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  6. 【翻译】MongoDB指南/CRUD操作(一)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...

  7. ASP.NET Core Web API Cassandra CRUD 操作

    在本文中,我们将创建一个简单的 Web API 来实现对一个 “todo” 列表的 CRUD 操作,使用 Apache Cassandra 来存储数据,在这里不会创建 UI ,Web API 的测试将 ...

  8. MongoDB的CRUD操作

    1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...

  9. 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】

    一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...

随机推荐

  1. mac 下对 iterm 终端 设置代理

    vi .profile export http_prox="http://xxxx:port" export https_proxy="http://xxxx:port& ...

  2. Java日期处理类

    1.Date java.util.Date 2.Calendar 日历类,通过getInstance()获取实例对象,可以获取年月日时分秒 3.SimpleDateFormat 日期格式化,forma ...

  3. 分析恶意驱动(进程启动apc注入dll)

    一.前言  用IDA也有好些时间了,以前就只会用F5功能玩无壳无保护的裸驱动,感觉太坑了,这两天就开始看网上大牛的逆向. 今天记录一下sudami曾经逆向过的fuck.sys.第一遍自己走的时候漏掉了 ...

  4. SQLite使用事务更新—by command

    public void SaveToDB(DataTable dt) { /* todo:sqlite没有提供批量插入的机制,需要通过事务处理 更新所有数据 * http://www.cnblogs. ...

  5. 下载Xml文件方法

    #region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...

  6. jQuery UI 多选下拉框插件:jquery-ui-multiselect

    前一个项目,由于项目需求,需要大量使用到下拉多选框,而由于本人又不会写有关 CSS 样式,所以,便上网找到了这个 jQuery 插件:jquery-ui-multiselect .该款插件提供了基本下 ...

  7. How Much Work Does it Take to be a Successful Mathematician?

    http://mathoverflow.net/questions/9799/how-much-work-does-it-take-to-be-a-successful-mathematician# ...

  8. Cocos2d-x——Cocos2d-x 屏幕适配新解【转载】

    Cocos2d-x 屏幕适配新解 本文出自[无间落叶](转载请保留出处):http://blog.leafsoar.com/archives/2013/05-10-19.html 为了适应移动终端的各 ...

  9. 中国软件开发project师之痛

    在最近的一次会议上,有高层谈到之前在中国觉得自己做得非常牛,但与美国同行接触后却发现与人家存在非常大的差距,这一点我在外企工作时也有过相同的体会.真正与外国同行接触后才会知道什么是差距,在这篇文章中我 ...

  10. python的一些总结3

    好吧 刚刚的2篇文章都很水.. 这篇 也是继续水 在 templates 右键新建 html 文件:如 index.html (输入以下代码) <!DOCTYPE html> <ht ...