一、SQLiteDatabase简介
Android提供了SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQLiteDAtabase对象,接下来就可通过SQLiteDatabase对象来操作数据库了。
SQLiteDatabase提供了如下静态方法来打开/创建一个文件的数据库。
- static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory,int flags);
- static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory);
- static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory);
在程序中获取SQLiteDatabase对象之后,接下来就可以调用SQLiteDatabase的方法来操作数据库了。
二、程序实例
下面的程序示范了如何在Android应用中操作SQLite数据库。该程序提供了两个文本框,用户可以在这两个文本框中输入内容,当用户单击“插入”按钮时,这两个文本框的内容将被插入到数据库当中。随后,程序读取数据库中的值,并在后面显示显示出来。
效果图(中间插入了一些空格)
代码实现
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"/>
<ListView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
line.xml
<?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="horizontal">
<EditText
android:id="@+id/my_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120dp"/>
<EditText
android:id="@+id/my_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.example.zhaoyc.dbtest;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
private Button btn_ok;
private EditText edt_title;
private EditText edt_content;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建或打开数据库
System.out.println("打印数据库安装路径:"+this.getFilesDir().toString()+"/my.db3");
/*
打印数据库安装路径:/data/data/com.example.zhaoyc.dbtest/files/my.db3
注意:该路经在在文件管理中找不到,属于系统文件夹,有两种方法可以查看
*/
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db3",null);
listView = (ListView)findViewById(R.id.show);
edt_title = (EditText)findViewById(R.id.edt_title);
edt_content = (EditText)findViewById(R.id.edt_content);
btn_ok = (Button)findViewById(R.id.btn_ok);
//
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取用户输入
String title = edt_title.getText().toString();
String content = edt_content.getText().toString();
System.out.println("title:"+title+"....content:"+content);
try {
System.out.println("try insert data...");
//执行insert语句插入数据
insertData(db,title,content);
//执行查询
Cursor cursor = db.rawQuery("select * from news_inf",null);
inflateList(cursor);
}catch (SQLiteException se) {
//执行DDL创建数据表
db.execSQL("create table news_inf(_id integer primary key autoincrement,"
+ "news_title varchar(50),"
+ "news_content varchar(255))");
//执行insert语句插入数据
insertData(db,title,content);
//执行查询
Cursor cursor = db.rawQuery("select * from news_inf",null);
inflateList(cursor);
}
}
});
}
private void inflateList(Cursor cursor) {
//填充SimpleCursorAdapter
SimpleCursorAdapter adapter =new SimpleCursorAdapter(
MainActivity.this,
R.layout.line,
cursor,
new String[]{"news_title","news_content"},
new int[]{R.id.my_title,R.id.my_content},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
);
//显示数据
listView.setAdapter(adapter);
}
private void insertData(SQLiteDatabase db, String title, String content) {
//执行插入语句
db.execSQL("insert into news_inf values(null,?,?)",
new String[]{title,content});
}
@Override
protected void onDestroy() {
super.onDestroy();
//退出程序时,关闭SQLiteDatabase
if (db!=null && db.isOpen()){
db.close();
}
}
}