SQLiteOpenHelper 是Android提供的一个管理数据库的工具类,可以用于管理数据库创建和版本更新.一般的用法是创建 SQLiteOpenHelper 的子类,并扩展它的onCreate(SQLiteDatabase db)方法和 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法.
常用方法如下:
下面一个实例来学习 SQLiteOpenHelper 的功能与用法.
package com.test.sqliteopenhelper;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* SQLiteOpenHelper 的功能与用法.
*/
public class MainActivity extends AppCompatActivity {
MyDatabaseHelper dbHelper;
Button insert = null;
Button search = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建 MyDatabaseHelper 对象指定数据库版本为 1,此处使用相对路径
// ..数据库文件自动会保存在程序的数据库文件夹的 database目录下
dbHelper = new MyDatabaseHelper(this, "myDict.db3", null, 1);
insert = (Button) findViewById(R.id.insert);
search = (Button) findViewById(R.id.search);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取用户输入信息
String word = ((EditText) findViewById(R.id.word)).getText().toString();
String detail = ((EditText) findViewById(R.id.detail)).getText().toString();
//插入生词记录
insertData(dbHelper.getReadableDatabase(), word, detail);
Toast.makeText(MainActivity.this, "添加生词成功!", Toast.LENGTH_SHORT).show();
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取用户输入的关键词
String key = ((EditText) findViewById(R.id.key)).getText().toString();
//执行查询
Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select * from dict where word like ? or detail like ? ",
new String[]{"%" + key + "%", "%" + key + "%"});
//创建一个 Bundle
Bundle bundle = new Bundle();
bundle.putSerializable("data", converCursorToList(cursor));
//创建一个 Intent
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
public ArrayList
<LinearLayout
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:orientation="vertical"
tools:context="com.test.sqliteopenhelper.MainActivity">
<EditText
android:id="@+id/word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="单词"/>
<EditText
android:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="详情"
android:lines="1"/>
<Button
android:id="@+id/insert"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/insert"/>
<EditText
android:hint="@string/key"
android:id="@+id/key"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search"
/>
<ListView
android:divider="#123"
android:dividerHeight="2px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show"
/>
LinearLayout>
这是一个对话框风格的activity
<activity android:name=".ResultActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog"
>
activity>
package com.test.sqliteopenhelper;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.List;
import java.util.Map;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
ListView listView = (ListView) findViewById(R.id.result);
Intent intent = getIntent();
//获取Intent 携带的数据
Bundle bundle = intent.getExtras();
List> mapList = (List>) bundle.getSerializable("data");
//设置一个adapter
SimpleAdapter adapter = new SimpleAdapter(
ResultActivity.this,
mapList,
R.layout.item,
new String[]{"word","detail"},
new int[]{R.id.word,R.id.detail});
listView.setAdapter(adapter);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:divider="?android:attr/dividerVertical"
android:showDividers="beginning"
android:dividerPadding="2dp"
>
<ListView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:editable="false"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/detail"
/>
<EditText
android:id="@+id/detail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:lines="3"
/>
LinearLayout>