android应用开发存储方式之SQLite(android自带的数据库)的一些个人理解

转载请注明出处: 

学习了android有一段时间了,然后谈谈自己对于android存储只SQLite的一些个人的见解吧,个人感觉是比较清晰的

首先SQLite语句和普通的SQL语句并没有什么不一样,可能只是写的时候的一些不同而已,而我在此便是将那些不同的地方用自己的理解进行描述而已,具体看下列的

文字 

 
1.SQLiteOpenHelper的使用,是一个第三方的管理类,帮助SQLite对数据进行管理
 
public class SDBhelper extends SQLiteOpenHelper {

private static final String name = "data.db";// 数据库的名字
private static final int version = 1;// 版本最小为1

// 指定了数据库的名称,版本信息
public SDBhelper(Context context) {

super(context, name, null, version);
}

// 一般是放置建表语句
public void onCreate(SQLiteDatabase database) {

String sql1 = "create table table1(_id integer primary key autoincrement not null)";
String sql2 = "create table table2(_id integer primary key autoincrement not null)";

database.execSQL(sql1);
database.execSQL(sql2);
}

// 升级的方法
public void onUpgrade(SQLiteDatabase database, int oldversion, int newversion) {

if (newversion > oldversion) {

String sql = "drop table if exists table1";
database.execSQL(sql);
onCreate(database);
}
}
}
 
以上是工具类的实现方法
 
 
2.SDBhelper 的使用:
 
SDBhelper helper = new SDBhelper(this);

//正常情况下,以下两种方法得到的数据库是一样的

//非正常情况下,即有明确的要求,或者是磁盘已经满了。便是只读或者是只写

SQLiteDatabase db1 = helper.getWriteableDatabase();

SQLiteDatabase db2 = helper.getReadableDatabase();
3.SQLite的实例
 
{

布局文件:

<?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="vertical" >

    <TextView
       android:layout_width="fill_parent"
         android:layout_height="wrap_content"
       android:background="@color/pink"
         android:gravity="center"
       android:text="@string/sqlite"
       android:textSize="25dp" />

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="20dp" >

           <EditText
           android:id="@+id/oldData"
           android:layout_width="0dp"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:hint="@string/data" />

           <EditText
           android:id="@+id/newData"
           android:layout_width="0dp"
           android:layout_height="fill_parent"
           android:layout_weight="1"
             android:hint="@string/dataNew" />
     </LinearLayout>

   <Button
       android:id="@+id/add"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlAdd" />

   <Button
       android:id="@+id/delete"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlDelete" />

   <Button
         android:id="@+id/deleteAll"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlDeleteAll" />

   <Button
       android:id="@+id/modify"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlmodify" />

    <Button
       android:id="@+id/search1"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlSearch1" />


   <Button
       android:id="@+id/searchAll"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_marginBottom="30dp"
       android:layout_marginTop="30dp"
       android:layout_weight="1"
       android:background="@color/orange"
       android:hint="@string/sqlSearch2" />

</LinearLayout>

}
{

操作实例:

public void onClick(View view) {

switch (view.getId()) {

case R.id.add://添加新的数据
str = oldData.getText().toString();
values.put("value", str);
database.insert("table1", "value=?", values);
break;

case R.id.delete://删除指定数据
str = oldData.getText().toString();
database.delete("table1", "value=?", new String[]{str});
break;
case R.id.deleteAll://删除所有的数据
database.execSQL("drop table if exists table1");
String sql1 = "create table table1(_id integer primary key autoincrement not null,"
+ "value text not null)";
database.execSQL(sql1);
break;

case R.id.modify://用指定数据更新原来指定的数据
str = oldData.getText().toString();
str_new = newData.getText().toString();
values.put("value", str_new);
database.update("table1", values, "value=?",
new String[] { str });
break;

case R.id.search1://查询指定数据
cursor = database.rawQuery("select *from table1", null);
str = oldData.getText().toString();
if (cursor == null)
Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
else
while (cursor.moveToNext()) {
if (cursor.getString(cursor.getColumnIndex("value"))
.contains(str))
result += cursor.getString(cursor
.getColumnIndex("value")) + '
';
}
ShowData();
break;

case R.id.searchAll://查询所有的数据
cursor = database.rawQuery("select *from table1", null);
if (cursor == null)
Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
else
while (cursor.moveToNext())
result += cursor.getString(cursor.getColumnIndex("value")) + '
';
ShowData();
break;

default:
break;
}
newData.setText("");
oldData.setText("");
}

private void ShowData() {

builder.setTitle(title);
builder.setMessage(result);
builder.show();
result = "";
 }

}
 

相信那些id命名也是相对比较规范的,如果有写的不好的地方,欢迎私信我,谢谢大家,共同进步吧

原文地址:https://www.cnblogs.com/huaixiaohai2015/p/5804955.html