今天趁着有时间,自己在网上找了相关的数据库操作代码,进行了一下练习,先上代码

main.xml文件

<RelativeLayout 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.example.sqlittest.MainActivity" > <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#0000ff" /> <LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" > <Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建数库"
android:textColor="#0000ff" /> <Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建表格"
android:textColor="#0000ff" /> <Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:textColor="#0000ff" />
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_above="@id/l1"
android:orientation="horizontal" >
>
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
android:textColor="#0000ff" /> <Button
android:id="@+id/bt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改数据"
android:textColor="#0000ff" /> <Button
android:id="@+id/bt6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看表格"
android:textColor="#0000ff" />
</LinearLayout> </RelativeLayout>

然后看练习代码

package com.example.sqlittest;

import android.R.string;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends ActionBarActivity implements OnClickListener {
TextView tv;
Button bt1, bt2, bt3, bt4, bt5, bt6, bt7;
SQLiteDatabase mDatabase; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mDatabase = SQLiteDatabase.openOrCreateDatabase(
"/data/data/com.example.sqlittest/test.db", null); initButton(); } private void initButton() {
tv = (TextView) findViewById(R.id.tv);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
bt6 = (Button) findViewById(R.id.bt6);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this); } @Override
public 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;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} // 创建数据库
private void createdb() {
mDatabase = SQLiteDatabase.openOrCreateDatabase(
"/data/data/com.example.sqlittest/test.db", null); } // 创建表格
private void createtabe() {
String stu_table = "create table testtable(_id integer primary key autoincrement,sname text,snumber text)";
mDatabase.execSQL(stu_table); } // 插入数据方式1
private void insert1() {
ContentValues contentValues = new ContentValues();
contentValues.put("sname", "zhangsan");
contentValues.put("snumber", "13166111082");
mDatabase.insert("testtable", null, contentValues); } // 插入数据方式2
private void insert2() {
String stu_sql = "insert into testtable(sname,snumber) values('xiaoming','01005')";
mDatabase.execSQL(stu_sql); } // 删除数据方式1
private void delete1() {
String string = "_id=?";
String[] string2 = { String.valueOf(1) };
mDatabase.delete("testtable", string, string2);
} // 删除数据方式2
private void delete2() {
String sql = "delete from testtable where _id = 2";
mDatabase.execSQL(sql); } // 修改数据方式1
private void change1() {
String string = "_id=?";
String[] string2 = { String.valueOf(1) };
ContentValues contentValues = new ContentValues();
contentValues.put("sname", "zhangsi");
mDatabase.update("testtable", contentValues, string, string2); } // 修改数据方式2
private void change2() {
String sql = "update testtable set snumber = 654321 where _id = 1";
mDatabase.execSQL(sql); } // 数据库查询
private String squre() {
StringBuffer stringBuffer = new StringBuffer();
Cursor cursor = mDatabase.query("testtable", null, null, null, null,
null, null);
// 判断游标是否为空
if (!cursor.moveToFirst()) {
return null;
}
for (int i = 0; i < cursor.getCount() - 1; i++) {
// 获得ID
cursor.moveToNext();
int id = cursor.getInt(0);
// 获得用户名
int name_id = cursor.getColumnIndexOrThrow("sname");
String username = cursor.getString(name_id);
// 获得密码
int number_id = cursor.getColumnIndexOrThrow("snumber");
Log.e("123",
"" + name_id + " " + number_id + " count"
+ cursor.getCount());
String password = cursor.getString(number_id);
stringBuffer.append("name_id:" + name_id + "\n" + " name:"
+ username + "\n" + " number:" + password + "\n"); } return stringBuffer.toString();
} @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bt1:
// createdb();
break;
case R.id.bt2:
// createtabe();
break;
case R.id.bt3:
insert1();
insert2();
break;
case R.id.bt4:
delete1();
delete2();
break;
case R.id.bt5:
change1();
change2();
break;
case R.id.bt6:
tv.setText(squre());
break;
default:
break;
}
}
}

结果如下

在做练习的时候,遇到了以下几个问题:

1)当创建数据库的时候,刚开始使用的是

mDatabase = SQLiteDatabase.openOrCreateDatabase("test.db",null);

结果报错了。所以我们可以得出的结论是,这个数据库创建,必须是指定的应用路径

2)当进行遍历的时候,开始使用的是

for (int i = 0; i < cursor.getCount() - 1; i++) {
cursor.move(i);
}

报错,后来发现问题了,原来是数据库并不会自动更新id,也就是说,如果你删除了id=3的行,那么这一行就永久消失了,id只会增加,不会自动重新排序

3)当我们删除一个数据的时候,我们查看数据库,会发现表格内容如下

|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|
|xiaoming|
|zhangsan|

另外,数据库id永远是从1开始,而不是0,这是问题2报错的原因

SQLite-SQLiteDatabase 数据库实例练习的更多相关文章

  1. Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase

    数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...

  2. (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解

    原文:http://blog.csdn.net/lidew521/article/details/8655229 1.SQLite介绍:最大特点是,无数据类型;除了可以使用文件或SharedPrefe ...

  3. sqlite嵌入式数据库C语言基本操作(2)

    :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...

  4. Android SQLite 的简单实例

    1.前言: 今天再一次去蹭了一下某老师的android课,这一次讲的是Android的SQLite的使用,老师当场讲解了他自己做的例子. 回来之后,我春心萌动,不得不拿着参考资料再做了一个类似的例子, ...

  5. 关于PDF.NET开发框架对Mysql Sqlite PostgreSQL数据库分页支持的个人看法

    关于PDF.NET开发框架的名字由来  在设计www.pwmis.com站点的时候,考虑到架构的兼容性和将来升级的可能性,最重要的是没有足够的时间去为网站添加和维护很多复杂的程序,所以在借鉴前人成功经 ...

  6. sqlite创建数据库并创建一个表

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  7. SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite-attach-database.html)

    SQLite 附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何一个.SQLite 的 ATTACH DTABASE 语句是用来选择一个特定的数据库,使用该命令后,所有的 ...

  8. SQLite 分离数据库(http://www.w3cschool.cc/sqlite/sqlite-detach-database.html)

    SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的.如果同一个数据库文件已经被附加上 ...

  9. SQLite 创建数据库(http://www.w3cschool.cc/sqlite/sqlite-create-database.html)

    SQLite 创建数据库 SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库.您不需要任何特殊的权限即可创建一个数据. 语法 sqlite3 命令的基本语法如下: $sqlit ...

  10. Unity3D游戏开发之SQLite让数据库开发更简单

    各位朋友大家好.欢迎大家关注我的博客,我是秦元培,我是博客地址是http://blog.csdn.net/qinyuanpei.在经历了一段时间的忙碌后,博主最终有时间来研究新的东西啦,今天博客向和大 ...

随机推荐

  1. CSS响应式布局到底是什么?

    响应式布局是最近几年在前端开发中非常火热的词,它是相对于固定像素大小的网页而言的,那么CSS响应式布局到底是什么?顾名思义,响应式布局就是网页能够响应各种各样不同分辨率大小的设备,能够将网页很好的呈献 ...

  2. JS之预编译和执行顺序(全局和函数)

    预编译的两种情况 全局: 1.全局 直接是script标签中的代码,不包括函数执行 执行前: 1.首先生成一个GO(global object)对象,看不到,但是可以模拟出来用来分析 2.分析变量声明 ...

  3. react基础用法二(组件渲染)

    react基础用法二(组件渲染) 如图所示组件可以是函数 格式:function 方法名(){ return <标签>内容</标签>} 渲染格式: <方法名 />  ...

  4. 使用 HTML5 canvas制作拾色器

    自制的拾色器漂亮吧,哈哈 废话不多说直接上代码,希望可以帮到需要的朋友 <html><head>    <style>        .canvas_color{p ...

  5. 如何形象的解释 webhook 这个词

    就是一个 callback,只不过 callback 的操作是发送指定的 HTTP request 给一个指定的地址. callback 就是由甲传给乙,乙处理完之后通知甲传过来的方法或者请求甲方的 ...

  6. hdoj 1159 Common Subsequence【LCS】【DP】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. golang-nw

    http://godoc.org/github.com/lonnc/golang-nw golang webkit golang walk

  8. Atcoder AGC 019 A,B

    A - Ice Tea Store Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You' ...

  9. HDU 4696 Answers 水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4696 由题意可知 1<=Ci<=2 而且图上一定有环 那么我们可以得出: 只要存在奇环(即Ci=1) ...

  10. Kali的源得数字验证问题

    装上之后第一件事就是执行apt-get update && apt-get upgrade,结果却出现了这样的错误 我添加的是中科大的更新源,在浏览器中是可以正常打开的: deb ht ...