特别强调一下,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
总结以上是内存溢出为你收集整理的Android *** 作SQLite数据库(初步)-在程序中删除数据库全部内容,希望文章能够帮你解决Android *** 作SQLite数据库(初步)-在程序中删除数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)