SQLite-SQLiteDatabase 数据库实例练习
今天趁着有时间,自己在网上找了相关的数据库操作代码,进行了一下练习,先上代码
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 数据库实例练习的更多相关文章
- Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase
数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...
- (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解
原文:http://blog.csdn.net/lidew521/article/details/8655229 1.SQLite介绍:最大特点是,无数据类型;除了可以使用文件或SharedPrefe ...
- sqlite嵌入式数据库C语言基本操作(2)
:first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...
- Android SQLite 的简单实例
1.前言: 今天再一次去蹭了一下某老师的android课,这一次讲的是Android的SQLite的使用,老师当场讲解了他自己做的例子. 回来之后,我春心萌动,不得不拿着参考资料再做了一个类似的例子, ...
- 关于PDF.NET开发框架对Mysql Sqlite PostgreSQL数据库分页支持的个人看法
关于PDF.NET开发框架的名字由来 在设计www.pwmis.com站点的时候,考虑到架构的兼容性和将来升级的可能性,最重要的是没有足够的时间去为网站添加和维护很多复杂的程序,所以在借鉴前人成功经 ...
- sqlite创建数据库并创建一个表
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite-attach-database.html)
SQLite 附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何一个.SQLite 的 ATTACH DTABASE 语句是用来选择一个特定的数据库,使用该命令后,所有的 ...
- SQLite 分离数据库(http://www.w3cschool.cc/sqlite/sqlite-detach-database.html)
SQLite 分离数据库 SQLite的 DETACH DTABASE 语句是用来把命名数据库从一个数据库连接分离和游离出来,连接是之前使用 ATTACH 语句附加的.如果同一个数据库文件已经被附加上 ...
- SQLite 创建数据库(http://www.w3cschool.cc/sqlite/sqlite-create-database.html)
SQLite 创建数据库 SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库.您不需要任何特殊的权限即可创建一个数据. 语法 sqlite3 命令的基本语法如下: $sqlit ...
- Unity3D游戏开发之SQLite让数据库开发更简单
各位朋友大家好.欢迎大家关注我的博客,我是秦元培,我是博客地址是http://blog.csdn.net/qinyuanpei.在经历了一段时间的忙碌后,博主最终有时间来研究新的东西啦,今天博客向和大 ...
随机推荐
- The method getDispatcherType() is undefined for the type HttpServletRequest错误解决方法
使用Eclipse Luna版本,jdk1.7和tomcat8.0开发JAVA EE应用.写一个简单的JSP部署后访问报JSP编译错误,具体错误信息如下: The method getDispatch ...
- 便捷编程-Xcode常用第三方插件 (随时更新)
Xcode工具插件 1.XAlign 让Xcode编辑器中的代码以多种方式瞬间对齐 地址:https://github.com/qfish/XAlign 2.VVDocumenter-Xcode 在X ...
- JavaScript-原型&原型链&原型继承
JavaScript-原型&原型链&原型继承 JavaScript的原型是一个重要的知识点,很多扩展应用都是从原型出发的.要说原型,我们先简单说一下函数创建过程.上一篇文章用闭包实现类 ...
- 【Codeforces Round #424 (Div. 2) C】Jury Marks
[Link]:http://codeforces.com/contest/831/problem/C [Description] 有一个人参加一个比赛; 他一开始有一个初始分数x; 有k个评委要依次对 ...
- Android学习总结(1)——好的 Android 开发习惯
Android编码规范 java代码中不出现中文,最多注释中可以出现中文: 局部变量命名.静态成员变量命名:只能包含字母,单词首字母出第一个都为大写,其他字母都为小写: 常量命名:只能包含字母和 ,字 ...
- RHEL7.1安装VNC
1.安装包 yum install vnc* -y 2.创建password vncserver 3.创建參数文件 [root@single ~]# cp /lib/systemd/system/vn ...
- Project Euler :Problem 54 Poker hands
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the ...
- sql server 内置MD5加密函数
http://blog.csdn.net/rookie_liu_ToFly/article/details/53116932 select right(sys.fn_VarBinToHexStr(HA ...
- 2017国家集训队作业[arc076d/f][Exhausted?]
2017国家集训队作业[arc076d/f][Exhausted?] 题意: 有\(N\)个人,\(M\)把椅子,给出\(...L_i.R_i\)表示第\(i\)个人可以选择编号为\(1\sim ...
- Lusac定理
转载大佬的模版:http://www.cnblogs.com/vongang/archive/2012/12/02/2798138.html