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

    在编程中经常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,能够支持Windows/Linux/Unix等主流操作系统,同时能够跟很多程序语言如C#、PHP、Java等相结合.下面先回顾SQL的基本语句,再讲述Android的基本操作.

一. adb shell回顾SQL语句

    首先,我感觉自己整个大学印象最深的几门课就包括《数据库》,所以想先回顾SQL增删改查的基本语句.而在Android SDK中adb是自带的调试工具,它存放在sdk的platform-tools目录下,通过adb shell可以进入设备控制台,操作SQL语句.

G:
cd G:\software\Program software\Android\adt-bundle-windows-x86_64-20140321\sdk\platform-tools
adb shell
cd /data/data/com.example.sqliteaction/databases/
sqlite3 StuDatabase.db
.table
.schema

    如下所示我先创建了SQLiteAction工程,同时在工程中创建了StuDatabase.db数据库.输入adb shell进入设备控制台,调用"sqlite3+数据库名"打开数据库,如果没有db文件则创建.

    然后如下图所示,可以输入SQL语句执行增删改查.注意很容易写错SQL语句,如忘记")"或结束";"导致cmd中调用出错.

--创建Teacher表
create table Teacher (id integer primary key, name text);
--向表中插入数据
insert into Teacher (id,name) values('10001', 'Mr Wang');
insert into Teacher (id,name) values('10002', 'Mr Yang');
--查询数据
select * from Teacher;
--更新数据
update Teacher set name='Yang XZ' where id=10002;
--删除数据
delete from Teacher where id=10001;

二. SQLite数据库操作

   下面讲解使用SQLite操作数据库:
    1.创建打开数据库
    使用openOrCreateDatabase函数实现,它会自动检测是否存在该数据库,如果存在则打开,否则创建一个数据库,并返回一个SQLiteDatabase对象.
    2.创建表
    通过定义建表的SQL语句,再调用execSQL方法执行该SQL语句实现建立表.

//创建学生表(学号,姓名,电话,身高) 主键学号
public static final String createTableStu = "create table Student (" +
		"id integer primary key, " +
		"name text, " +
		"tel text, " +
		"height real)";
//SQLiteDatabase定义db变量
db.execSQL(createTableStu);

    3.插入数据
    使用insert方法添加数据,其实ContentValues就是一个Map,Key字段名称,Value值.
    SQLiteDatabase.insert(
        String table,         //添加数据的表名
        String nullColumnHack,//为某些空的列自动复制NULL
        ContentValues values  //ContentValues的put()方法添加数据
    );


//方法一
SQLiteDatabase db = sqlHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", "10001");
values.put("name", "Eastmount");
values.put("tel", "15201610000");
values.put("height", "172.5");
db.insert("Student", null, values);
//方法二
public static final String insertData = "insert into Student (" +
		"id, name, tel, height) values('10002','XiaoMing','110','175')";
db.execSQL(insertData);

   
4.删除数据
    使用delete方法删除表中数据,其中sqlHelper是继承SQLiteDatabase自定义类的实例.
    SQLiteDatabase.delete(
        String table,       //表名
        String whereClause, //约束删除行,不指定默认删除所有行
        String[] whereArgs  //对应数据
    );

//方法一 删除身高>175cm
SQLiteDatabase db = sqlHelper.getWritableDatabase();
db.delete("Student", "height > ?", new String[] {"175"});
//方法二
String deleteData = "DELETE FROM Student WHERE height>175";
db.execSQL(deleteData);

   
5.更新数据
    使用update方法可以修改数据,SQL+execSQL方法就不在叙述.

//小明的身高修改为180
SQLiteDatabase db = sqlHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("height", "180");
db.update("Student", values, "name = ?", new String[] {"XiaoMing"});

    6.其他操作


   
下面是关于数据库的其他操作,其中包括使用SQL语句执行,而查询数据Query方法由于涉及ListView显示,请见具体实例.

//关闭数据库
SQLiteDatabase.close();
//删除表 执行SQL语句
SQLiteDatabase.execSQL("DROP TABLE Student");
//删除数据库
this.deleteDatabase("StuDatabase.db");
//查询数据
SQLiteDatabase.query();

三. 数据库操作简单实例

   
显示效果如下图所示:
  
    首先,添加activity_main.xml文件布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.touchimagetest.MainActivity"
    tools:ignore="MergeRootFrame" >
     <!-- 顶部  -->
    <RelativeLayout
        android:id="@+id/MyLayout_top"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true" >
        <!-- 标题 -->
        <LinearLayout
			android:orientation="horizontal"
		  	android:layout_width="fill_parent"
		  	android:layout_height="fill_parent"
		  	android:gravity="center" >
		  	<TextView
		   		android:layout_width="wrap_content"
		   		android:layout_height="wrap_content"
		   		android:layout_weight="1"
		   		android:gravity="center"
		   		android:textSize="20sp"
		   		android:text="学号" />
		   	<TextView
		   		android:layout_width="wrap_content"
		   		android:layout_height="wrap_content"
		   		android:layout_weight="1"
		   		android:gravity="center"
		   		android:textSize="20sp"
		   		android:text="姓名" />
		  	<TextView
		   		android:layout_width="wrap_content"
		   		android:layout_height="wrap_content"
		   		android:layout_weight="1"
		   		android:gravity="center"
		   		android:textSize="20sp"
		   		android:text="电话" />
		  	<TextView
		   		android:layout_width="wrap_content"
		   		android:layout_height="wrap_content"
		   		android:layout_weight="1"
		   		android:gravity="center"
		   		android:textSize="20sp"
		   		android:text="身高" />
		</LinearLayout>
    </RelativeLayout>
    <!-- 底部按钮 -->
    <RelativeLayout
        android:id="@+id/MyLayout_bottom"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:gravity="center">
        <LinearLayout
			android:orientation="vertical"
		  	android:layout_width="fill_parent"
		  	android:layout_height="fill_parent"
		  	android:layout_alignParentBottom="true" >
	        <LinearLayout
				android:orientation="horizontal"
			  	android:layout_width="fill_parent"
			  	android:layout_height="40dp"
			  	android:gravity="center" >
			  	<EditText
			  	    android:id="@+id/edit_id"
			   		android:layout_width="wrap_content"
			   		android:layout_height="wrap_content"
			   		android:layout_weight="1"
			   		android:gravity="center"
			   		android:textSize="20sp"
			   		android:hint="学号" />
			   	<EditText
			  	    android:id="@+id/edit_name"
			   		android:layout_width="wrap_content"
			   		android:layout_height="wrap_content"
			   		android:layout_weight="1"
			   		android:gravity="center"
			   		android:textSize="20sp"
			   		android:hint="姓名" />
			  	<EditText
			  	    android:id="@+id/edit_tel"
			   		android:layout_width="wrap_content"
			   		android:layout_height="wrap_content"
			   		android:layout_weight="1"
			   		android:gravity="center"
			   		android:textSize="20sp"
			   		android:hint="电话" />
			  	<EditText
			  	    android:id="@+id/edit_height"
			   		android:layout_width="wrap_content"
			   		android:layout_height="wrap_content"
			   		android:layout_weight="1"
			   		android:gravity="center"
			   		android:textSize="20sp"
			   		android:hint="身高" />
			</LinearLayout>
	        <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"
	            android:orientation="horizontal" >
	           	<Button
	                android:id="@+id/button1"
	                android:layout_width="wrap_content"
	                android:layout_height="match_parent"
	                android:layout_weight="1"
	                android:text="创表" />
	           <Button
	                android:id="@+id/button2"
	                android:layout_width="wrap_content"
	                android:layout_height="match_parent"
	                android:layout_weight="1"
	                android:text="插入" />
	         	<Button
	                android:id="@+id/button3"
	                android:layout_width="wrap_content"
	                android:layout_height="match_parent"
	                android:layout_weight="1"
	                android:text="删除" />
	           	<Button
	                android:id="@+id/button4"
	                android:layout_width="wrap_content"
	                android:layout_height="match_parent"
	                android:layout_weight="1"
	                android:text="更新" />
	             <Button
	                android:id="@+id/button5"
	                android:layout_width="wrap_content"
	                android:layout_height="match_parent"
	                android:layout_weight="1"
	                android:text="查询" />
	          </LinearLayout>
	      </LinearLayout>
    </RelativeLayout>
    <!-- 显示列表 -->
    <RelativeLayout
        android:id="@+id/Content_Layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/MyLayout_bottom"
        android:layout_below="@id/MyLayout_top"
        android:background="#EFDFDF" >
		<!-- 显示表内容 -->
        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >
        </ListView>
    </RelativeLayout>
</RelativeLayout>  

   
然后是在res/layout中添加ListView显示的stu_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/stu_id"
   		android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
   		android:layout_weight="1"
   		android:textSize="20sp" />
   	<TextView
   	    android:id="@+id/stu_name"
   		android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
   		android:layout_weight="1"
   		android:textSize="20sp" />
  	<TextView
  	    android:id="@+id/stu_tel"
   		android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
   		android:layout_weight="1"
   		android:textSize="20sp" />
  	<TextView
  	    android:id="@+id/stu_height"
   		android:layout_width="wrap_content"
   		android:layout_height="wrap_content"
   		android:layout_weight="1"
   		android:textSize="20sp" />
</LinearLayout>

   
再次,添加自定义类MySQLiteOpenHelper:

//添加自定义类 继承SQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {

	public Context mContext;

	//创建学生表(学号,姓名,电话,身高) 主键学号
	public static final String createTableStu = "create table Student (" +
			"id integer primary key, " +
			"name text, " +
			"tel text, " +
			"height real)";

	//抽象类 必须定义显示的构造函数 重写方法
	public MySQLiteOpenHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub
		arg0.execSQL(createTableStu);
		Toast.makeText(mContext, "Created", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		arg0.execSQL("drop table if exists Student");
		onCreate(arg0);
		Toast.makeText(mContext, "Upgraged", Toast.LENGTH_SHORT).show();
	}
}

   
最后是MainActivity.java文件,代码如下:

public class MainActivity extends Activity {

	//继承SQLiteOpenHelper类
	private MySQLiteOpenHelper sqlHelper;
	private ListView listview;
	private EditText edit1;
	private EditText edit2;
	private EditText edit3;
	private EditText edit4;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sqlHelper = new MySQLiteOpenHelper(this, "StuDatabase.db", null, 2);
        //建立新表
        Button createBn = (Button) findViewById(R.id.button1);
        createBn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		sqlHelper.getWritableDatabase();
        	}
        });
        //插入数据
        Button insertBn = (Button) findViewById(R.id.button2);
        edit1 = (EditText) findViewById(R.id.edit_id);
        edit2 = (EditText) findViewById(R.id.edit_name);
        edit3 = (EditText) findViewById(R.id.edit_tel);
        edit4 = (EditText) findViewById(R.id.edit_height);
        insertBn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		SQLiteDatabase db = sqlHelper.getWritableDatabase();
        		ContentValues values = new ContentValues();
        		/*
        		//插入第一组数据
        		values.put("id", "10001");
        		values.put("name", "Eastmount");
        		values.put("tel", "15201610000");
        		values.put("height", "172.5");
        		db.insert("Student", null, values);
        		*/
        		values.put("id", edit1.getText().toString());
        		values.put("name", edit2.getText().toString());
        		values.put("tel", edit3.getText().toString());
        		values.put("height", edit4.getText().toString());
        		db.insert("Student", null, values);
        		Toast.makeText(MainActivity.this, "数据插入成功", Toast.LENGTH_SHORT).show();
        		edit1.setText("");
        		edit2.setText("");
        		edit3.setText("");
        		edit4.setText("");
        	}
        });
        //删除数据
        Button deleteBn = (Button) findViewById(R.id.button3);
        deleteBn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		SQLiteDatabase db = sqlHelper.getWritableDatabase();
        		db.delete("Student", "height > ?", new String[] {"180"});
        		Toast.makeText(MainActivity.this, "删除数据", Toast.LENGTH_SHORT).show();
        	}
        });
        //更新数据
        Button updateBn = (Button) findViewById(R.id.button4);
        updateBn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		SQLiteDatabase db = sqlHelper.getWritableDatabase();
        		ContentValues values = new ContentValues();
        		values.put("height", "180");
        		db.update("Student", values, "name = ?", new String[] {"XiaoMing"});
        		Toast.makeText(MainActivity.this, "更新数据", Toast.LENGTH_SHORT).show();
        	}
        });
        //查询数据
        listview = (ListView) findViewById(R.id.listview1);
        Button selectBn = (Button) findViewById(R.id.button5);
        selectBn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		try {
	        		SQLiteDatabase db = sqlHelper.getWritableDatabase();
	        		//游标查询每条数据
	        		Cursor cursor = db.query("Student", null, null, null, null, null, null);
	        		//定义list存储数据
	        		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	        		//适配器SimpleAdapter数据绑定
	        		//错误:构造函数SimpleAdapter未定义 需把this修改为MainActivity.this
	        		SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.stu_item,
	        				new String[]{"id", "name", "tel", "height"},
	        				new int[]{R.id.stu_id, R.id.stu_name, R.id.stu_tel, R.id.stu_height});
	        		//读取数据 游标移动到下一行
	        		while(cursor.moveToNext()) {
	        			Map<String, Object> map = new HashMap<String, Object>();
	        			map.put( "id", cursor.getString(cursor.getColumnIndex("id")) );
	        			map.put( "name", cursor.getString(cursor.getColumnIndex("name")) );
	        			map.put( "tel", cursor.getString(cursor.getColumnIndex("tel")) );
	        			map.put( "height", cursor.getString(cursor.getColumnIndex("height")) );
	        			list.add(map);
	        		}
	        		listview.setAdapter(adapter);
        		}
        		catch (Exception e){
        			Log.i("exception", e.toString());
        		}
        	}
        });
    }
}

    PS:希望文章对大家有所帮助,文章是关于SQLite的基础操作,而且没有涉及到数据库的触发器、存储过程、事务、索引等知识,网上也有很多相关的资料.同时现在有门课程《数据库高级技术与开发》,故作者当个在线笔记及基础讲解吧!这篇文章有一些不足之处,但作为基础文章还是不错的.
    下载地址:http://download.csdn.net/detail/eastmount/8159881
    主要参考:
    1.郭霖大神的《第一行代码Android》
    2.android中的数据库操作By:nieweilin
(By:Eastmount 2014-11-15 夜2点 http://blog.csdn.net/eastmount/)

时间: 2024-08-07 01:27:34

[Android] SQLite数据库之增删改查基础操作的相关文章

JAVA访问数据库之增删改查(CRUD)

上一篇博客介绍了如何使用JAVA连接数据库,那么这一篇将继续为大家介绍如何使用JDBC对数据库的增删改查(CRUD)操作. 这一篇博客中的示例将使用上一篇中生成的H2数据库文件. 查询 查询在数据库的操作中是很重要的,我们把数据保存在数据库中,就是为了我们在需要的时候能够快速.高效的查询出来. /** * 查询 * * @throws Exception */ public void query() throws Exception { Connection conn = getConnecti

MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

[正文] 这一周状态不太好,连续打了几天的点滴,所以博客中断了一个星期,现在继续. 我们在之前的几篇文章中学习了JDBC对数据库的增删改查.其实在实际开发中,一般都是使用第三方工具类,但是只有将之前的基础学习好了,在使用开源工具的时才能得心应手.如果对JDBC基础不太清楚,或者对本文看不太懂,建议先回顾一下本人之前的几篇和"MySQL数据库学习笔记"相关的文章.但是不管怎样,今后如果用到了数据库的增删改查,肯定是这篇文章中的代码用的最多. 一.DbUtils简介: DBUtils是ap

急求一个Spring+Hibernate对MySQL数据库进行增删改查的例子!!!!

问题描述 现急求一个Spring+Hibernate对MySQL数据库进行增删改查的例子,例子比较简单也可以,主要就是要对数据库进行增删改查操作.如果哪位有的话请发到邮箱(395897780@qq.com),或者告诉我网址也行!最好是能连工程一起打包下载的,能够运行的,由于刚刚开始学,想下个完整的下来,怎样便于分析!!!本人在这里先表示感谢!!!!! 解决方案 解决方案二:其实不用这样,你用工具做,其中的包就可以生成的,注意:Hibernate一定要托管给Spring:之后以前Hibernate

Servlet实现对SQLServer数据库的增删改查(含工程源码)

本文实现了用MyEclipse,编写Servlet,实现对SQLServer数据库的增删改查,适合新手入门,文末提供工程文件源码下载. 1.新建数据库test以及表users 表users共四列(id,name,psd,tel) 具体操作步骤见上篇博文.具体操作步骤 2.新建工程Web Project工程0623p 3. 编辑WebRoot目录下的index.jsp 创建表单(序号.账号.密码.电话.操作),并读取当前数据库内容. 注意: 此处要导入sql_data.java包用于连接数据库(

java-不用数据库实现增删改查

问题描述 不用数据库实现增删改查 用java 代码实现 在内存中如何保存对象,修改对象的属性,删除一个对象 解决方案 保存对象:既然保存就是要存盘了,所以不可能只用内存,因为内存没有持久化.如果要保存对象到硬盘,可以用序列化(实现接口). 修改对象的属性:在有操作权限的情况下可以直接"对象.属性"修改,要是对象提供修改属性的方法也可以直接调用.如果又没访问权限,这--我猜是可行的. 删除对象:是回收对象所占的资源吧?java没有delete这种操作符,它是自动垃圾回收的,不同的虚拟机自

js 如何实现对数据库的增删改查_javascript技巧

JavaScript操作数据库JS操作Access数据库,跟其他语言操作差不多,总结了一下习惯代码,仅供参考学习. 现在在F盘有文件abc.mdf,表名为Student,一共2个字段,Id数字类型主键,stuName文本类型,现对该表进行增删改查的操作: 1.查询 复制代码 代码如下: <HTML> <HEAD> <TITLE>数据查询</TITLE> <Script > var conn = new ActiveXObject("AD

.net链接另一台电脑的mysql数据库进行增删改查全过程?

问题描述 .net链接另一台电脑的mysql数据库进行增删改查全过程?求范例求源代码 解决方案 解决方案二:远程数据库连接!解决方案三:好办啊.传参数,改配置文件地址.解决方案四:引用1楼hou306010849的回复: 远程数据库连接! 改一下你原来的程序里面的数据库连接字符串就行了

php中PDO方式实现数据库的增删改查

  PDO是mysql数据库操作的一个公用类了,我们不需要进行自定类就可以直接使用pdo来操作数据库了,但是在php默认配置中pdo是未开启所以我们必须先在php.ini中开启它才可以使用. 需要开启php的pdo支持,php5.1以上版本支持 实现数据库连接单例化,有三要素 静态变量.静态实例化方法.私有构造函数 DPDO.php ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

[正文] 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. DAO模式实际上是两个模式的组合,即Data Accessor (数据访问者)模式和 Active Domain Object(领域对象)模式.Data Accessor 模式实现了数据访问和业务逻辑的分离:Active Domain Object 模式实现了业务数据的对象化封装. 需要注意的是,DAO设计模式是Java EE中的设计模式