.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.application3.DataActivity2"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="创建数据库"
android:id="@+id/bt_bt1"
android:onClick="bt_bt1onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="升级数据库"
android:id="@+id/bt_bt2"
android:onClick="bt_bt2onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加数据"
android:id="@+id/bt_bt3"
android:onClick="bt_bt3onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据"
android:id="@+id/bt_bt4"
android:onClick="bt_bt4onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改数据"
android:id="@+id/bt_bt5"
android:onClick="bt_bt5onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"
android:id="@+id/bt_bt6"
android:onClick="bt_bt6onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="事务操作"
android:id="@+id/bt_bt7"
android:onClick="bt_bt7onClick"/> </LinearLayout>

.java

package com.hanqi.application3;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast; public class DataActivity2 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
}
//继承SQLiteOpenHelper
class DBHelper extends SQLiteOpenHelper
{
//构造方法
//name 数据库名
//version数据库版本号
public DBHelper(String name, int version) {
//调用父类的构造方法,写在第一行
super(DataActivity2.this, name, null, version);
}
//回调方法
//在创建数据库时调用
//什么时候创建数据库:连接数据库的时候,如果数据文件不存在
//只调用一次
@Override
public void onCreate(SQLiteDatabase db) { //1.创建数据库的语句
String creatTable = "create table user1 (_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,name varchar,age int)";
db.execSQL(creatTable); //2.初始化数据
ContentValues cv= new ContentValues();
cv.put("name","tom");
cv.put("age", 30);
//如果不成功返回-1
//1.第一个参数是表名,第二个参数空列的默认值,第三个字段和要插入值key/value得集合
long l = db.insert("user1",null,cv); Toast.makeText(DataActivity2.this, "id="+l, Toast.LENGTH_SHORT).show(); }
//升级数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//执行升级
// update user1 set age = 30 where _id = ?
//? 是占位符,然后给占位符赋值
ContentValues cv= new ContentValues(); cv.put("age", 30);
int rowcount = db.update("user1",cv,"_id=? and name=?",new String[]{"1","tom"}); Log.e("TAG","rowcount = "+rowcount); }
}
//创建数据库
public void bt_bt1onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",1);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase();
sdd.close(); }
//升级数据库
public void bt_bt2onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",2);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase();
sdd.close(); }
//插入数据
public void bt_bt3onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",2);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("name","tom1");
cv.put("age", 15);
long _id = sdd.insert("user1", null, cv);
Log.e("TAG","_id= "+_id); sdd.close(); }
//查询数据库
public void bt_bt4onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",2);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase();
// 全表查询
//返回游标
Cursor cursor = sdd.query("user1", null, null, null, null, null, null); //Cursor 一开始会定位在第一条数据的上方
// 移动游标到数据的上面,提取数据后,再继续移动
while (cursor.moveToNext())
{
long _id =cursor.getLong(cursor.getColumnIndex("_id"));
String name = cursor.getString(1);
int age = cursor.getInt(2); Log.e("TAG","_id = "+_id+"name = "+name+"age= "+age);
}
cursor.close();
sdd.close(); }
//修改数据
public void bt_bt5onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",2);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("name", "tom3"); int count = sdd.update("user1",cv,"_id >= 3",null);
Log.e("TAG", "Updatecount= " + count); sdd.close(); }
//删除数据
public void bt_bt6onClick(View v)
{
//创建
DBHelper dh = new DBHelper("test.db",2);
//获取数据库实例
SQLiteDatabase sdd = dh.getReadableDatabase(); int count = sdd.delete("user1","_id = 4",null);
Log.e("TAG", "Deletcount= " + count); sdd.execSQL("delect from user1 where _id=3"); sdd.close(); }
public void bt_bt7onClick(View v) {
//创建
DBHelper dh = new DBHelper("test.db", 2);
//获取数据库实例
SQLiteDatabase sdd = dh.getWritableDatabase();
//链接
sdd = SQLiteDatabase.openOrCreateDatabase("test.db",null);
//判断是否建表了,是否升级了
try { //1.开启事务
sdd.beginTransaction(); ContentValues cv = new ContentValues(); cv.put("age", "40"); int count = sdd.update("user1", cv, "_id = 1", null);
//抛出异常
boolean b = true;
if (b) {
//throw new RuntimeException("出现异常");
}
count += sdd.update("user1", cv, "_id = 2", null);
Log.e("TAG", "Updatecount= " + count);
//2.设置事务执行成功
sdd.setTransactionSuccessful();
//提交
}
catch (Exception e)
{
//回滚
e.printStackTrace(); Toast.makeText(DataActivity2.this, "异常信息"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
//一定会被执行的代码
finally { //3.结束事务,1)提交 2)回滚;
sdd.endTransaction(); sdd.close();
}
} }

andorid SQLite数据库的增删改查 和事务操作的更多相关文章

  1. android 对sqlite数据库的增删改查等各种操作

    转载:http://blog.csdn.net/vrix/article/details/6717090 package com.sqlite.main; import java.io.File; i ...

  2. Android学习---SQLite数据库的增删改查和事务(transaction)调用

    上一篇文章中介绍了手工拼写sql语句进行数据库的CRUD操作,本文将介绍调用sqlite内置的方法实现CRUD操作,其实质也是通过拼写sql语句. 首先,创建一个新的android项目: 其次,查看代 ...

  3. Android中Sqlite数据库进行增删改查

    今天这篇文章写Sqlite数据库,通过一个小案例来完整讲一下数据库常见的CRUD操作. 先对知识点总结: SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHel ...

  4. Android下利用SQLite数据库实现增删改查

    1: 首先介绍如何利用adb查看数据库 1: adb shell 2: cd /data/data/包名/databases 3:  sqlite3 数据库 4   接下来就可以进行数据库的sql语法 ...

  5. Android对Sqlite数据库的增删改查

    SqLite 数据库 Google 为我们提供了sqlite相关的api SqLiteOpenHelper 这是一个抽象的类 如果想要使用的话,需要其他的类去继承他 SqLiteDatabase 类 ...

  6. [Android] SQLite数据库之增删改查基础操作

        在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Un ...

  7. greendao对SQLite数据库的增删改查操作

    利用greendao操作数据库时,都是以对象或者对象的list来进行增删改查的操作,操作的结果都是用一个list来接收的!!! 1.增加一条记录 Stu stu01=new Stu();stu01.s ...

  8. SQLite数据库以及增删改查的案例

    Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...

  9. Android,java,php开发最基本的知识,mysql sqlite数据库的增删改查代理,sql语句

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985转载请说明出处. 下面是代码: 增加:insert into 数据表(字段1,字段2,字段3) valu ...

随机推荐

  1. .Net , 请取服务器上的文件

    public class IdentityScope : IDisposable { /// <summary> /// 登录一个新用户 /// </summary> /// ...

  2. WPF外包公司——北京动点飞扬软件:开发企业WPF项目需要掌握些什么

    做为企业开发一个WPF项目,对于很多不熟悉微软WPF技术和XAML语言开发团队而言,北京动点飞扬在此给各位一点建议: 1.首先开发团队要整体对于XAML和WPF的运作机制熟悉. 2.开发人员起码要会用 ...

  3. Hbase集群搭建及所有配置调优参数整理及API代码运行

    最近为了方便开发,在自己的虚拟机上搭建了三节点的Hadoop集群与Hbase集群,hadoop集群的搭建与zookeeper集群这里就不再详细说明,原来的笔记中记录过.这里将hbase配置参数进行相应 ...

  4. 一些实用的linux命令

    一直在用linux,可linux下的命令还是用得不是很熟悉,记录一下比较有用命令: ``和$()是一样的,都是用指令的执行结果来替换. linux下 echo hello    world => ...

  5. gcc-常见命令和错误

      一:编译过程的4个阶段:预处理,编译,汇编,链接; 1:最常用的方式 gcc hello.c -o hello 2:预处理后停止编译 gcc -E hello.c -o hello.i(.i通常为 ...

  6. view抖动效果

    1.使用属性动画 ViewPropertyAnimator.animate(webView).translationX(20).setInterpolator(new CycleInterpolato ...

  7. ChartControl

    <dxc:ChartControl Name="chartControl1">            <dxc:ChartControl.Diagram>  ...

  8. 神奇的C语言

    当然下面列出来的几点都是C的基础用法,只不过是这些用法可能平时不会被注意.所以很多东西第一次看到的时候,可能会觉得很怪异,但是细细想想就能很好的理解,也就能更好的清楚C语言的一些特性.但是在具体的编码 ...

  9. EXT学习之——Ext两个js之间的传参

    A  的js访问 B的js,并将A选择的guid的行传到  B的 js进行处理事项 A 的js 的写法var receiverFrom = new xxx.xxx子js方法体名 ({ parentCm ...

  10. NDK 的开发流程

    1.NDK开发所需要的工具 windows 需要在windows下的环境 把c代码打包成 手机能用的函数库 首先模拟手机的环境 1 NDK .sh linux 批处理文件 .bat windows 头 ...