特别强调一下,Android是怎么删除数据库的,因为sqlite没有提供drop database XX的指令,所以我现在是按文件来删除数据库
刚刚学习Android关于数据库的操作,现在就将我学习的这点知识汇总一下,高手绕道哈。
关于数据库操作无非就是增删改查,下面就将这几个模块实现了。
JAVA code
package cn.qiuzhping.study;
import java.io.File;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class sqliteStudy extends Activity {
private Button btn_create = null;
private Button btn_update = null;
private Button btn_insert = null;
private Button btn_query = null;
private Button btn_delete = null;
// private sqliteUtil sqliteUtil = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlite_study);
btn_create = (Button) findViewById(R.id.btn_create);
btn_create.setonClickListener(new Btn_Create());
btn_update = (Button) findViewById(R.id.btn_update);
btn_update.setonClickListener(new Btn_Update());
btn_insert = (Button) findViewById(R.id.btn_insert);
btn_insert.setonClickListener(new Btn_Insert());
btn_query = (Button) findViewById(R.id.btn_query);
btn_query.setonClickListener(new Btn_Query());
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_delete.setonClickListener(new Btn_Delete());
}
private class sqliteUtil extends sqliteOpenHelper {
private static final int VERSION = 1;// 正数
private String sql = "create table user(id int,name vachar(20))";
public sqliteUtil(Context context, String name, CursorFactory factory,
int version) {
// 从左到右依次是context是Activity、name数据库表名、factory可选的数据库游标工厂类,
// 当查询(query)被提交时,该对象会被调用来实例化一个游标。默认为null。version是数据版本号
super(context, name, factory, version);
}
public sqliteUtil(Context context, String name, int version) {// 调用4个参数的构造方法
this(context, name, null, VERSION);
}
public sqliteUtil(Context context, String name) {// 调用3个参数的构造方法
this(context, name, VERSION);
}
@Override
public void onCreate(sqliteDatabase arg0) {
// 建立数据库
Log.i("sqliteUtil onCreate", "建立数据库");
arg0.execsql(sql);
}
@Override
public void onUpgrade(sqliteDatabase arg0, int arg1, int arg2) {
// Todo Auto-generated method stub
Log.i("sqliteUtil onUpgrade", "更新数据库!!");
// arg0.execsql(sql);
}
}
public void deleteFile(File file) {
if (file.exists()) { // 判断文件是否存在
if (file.isFile()) { // 判断是否是文件
// 设置属性:让文件可执行,可读,可写
file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(true, false);
file.delete(); // delete()方法
} else if (file.isDirectory()) { // 否则如果它是一个目录
File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
this.deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
}
}
file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(true, false);
file.delete();
Log.i("deleteFile", file.getName() + "成功删除!!");
} else {
Log.i("deleteFile", file.getName() + "不存在!!!");
}
}
class Btn_Delete implements OnClickListener {
@Override
public void onClick(View arg0) {
sqliteUtil sqliteUtil = new sqliteUtil(sqliteStudy.this, "testDB1");
sqliteDatabase db = sqliteUtil.getWritableDatabase();
Log.i("Btn_Delete", "delete = " + db.delete("user", null, null));
File file1 = new File("/data/data/cn.qiuzhping.study/databases/testDB1");
deleteFile(file1);
File file2 = new File("/data/data/cn.qiuzhping.study/databases/testDB1-journal");
deleteFile(file2);
}
}
class Btn_Create implements OnClickListener {
@Override
public void onClick(View arg0) {
sqliteUtil sqliteUtil = new sqliteUtil(sqliteStudy.this, "testDB1");
sqliteDatabase db = sqliteUtil.getReadableDatabase();
}
}
class Btn_Update implements OnClickListener {
@Override
public void onClick(View arg0) {
sqliteUtil sqliteUtil = new sqliteUtil(sqliteStudy.this, "testDB1");
sqliteDatabase db = sqliteUtil.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "zhansan");
Log.i("Btn_Update ",
"update="
+ db.update("user", values, "id=?",
new String[] { "1" }));
}
}
class Btn_Insert implements OnClickListener {
@Override
public void onClick(View arg0) {
ContentValues values = new ContentValues();
for (int i = 1; i <= 20;) {
values.put("id", i);
values.put("name", "qiuzhping" + i);
sqliteUtil sqliteUtil = new sqliteUtil(sqliteStudy.this,
"testDB1");
sqliteDatabase db = sqliteUtil.getWritableDatabase();
Log.i("Btn_Insert",
"insert = " + db.insert("user", null, values));
i++;
}
}
}
class Btn_Query implements OnClickListener {
@Override
public void onClick(View arg0) {
ContentValues values = new ContentValues();
values.put("name", "qiuzhping");
sqliteUtil sqliteUtil = new sqliteUtil(sqliteStudy.this, "testDB1");
sqliteDatabase db = sqliteUtil.getReadableDatabase();
Cursor cursor = db.query("user", new String[] { "id", "name" },
null, null, null, null, null);
while (cursor.movetoNext()) {
int id = cursor.getInt(0);// 获取ID
String name = cursor.getString(1);// 获取name
Log.i("Btn_Query", "id = " + id + " name = " + name);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sqlite_study, menu);
return true;
}
}
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=".sqliteStudy" >
<Button
android:id="@+id/btn_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建数据库" />
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_create"
android:text="更新" />
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_update"
android:text="插入" />
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_insert"
android:text="查询" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_query"
android:text="删除数据库" />
</RelativeLayout>
转载于:https://my.oschina.net/qiuzhping/blog/611693
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。