android提供了一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQLiteDatabase对象,接下来就可以通过SQLiteDatabase对象来管理、操作数据库了。
SQLiteDatabase的execSQL()方法可执行任意的SQL语句,包括带占位符的SQL语句。但由于该方法没有返回值,因此一般用于执行DDL语句与DML语句;如果需要执行查询语句,则可调用SQLiteDatabase的rawQuerry(String sql ,String[] selectionArgs)方法。例如:
//执行插入语句
db.execSQL("insert into news values(null,?,?)",new String[]{title,content});
界面布局文件,提供两个文本框,用户可以在这两个文本框中输入内容,当用户单击“插入”按钮时这两个文本框的内容将会被插入数据库。
<?xml version="1.0" encoding="UTF-8"?>
-<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/title"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/content" android:lines="2"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/ok" android:text="@string/insert"/>
<ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/show"/>
</LinearLayout>
下面是activity:
public class MainActivity extends Activity
{
SQLiteDatabase db;
Button bn = null;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建或打开数据库(此处需要使用绝对路径)
db = SQLiteDatabase.openOrCreateDatabase(
this.getFilesDir().toString()
+ "/my.db3", null); // ①
listView = (ListView) findViewById(R.id.show);
bn = (Button) findViewById(R.id.ok);
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
// 获取用户输入
String title = ((EditText) findViewById(
R.id.title)).getText().toString();
String content = ((EditText) findViewById(R.id.content))
.getText().toString();
try
{
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 insertData(SQLiteDatabase db
, String title, String content) // ②
{
// 执行插入语句
db.execSQL("insert into news_inf values(null , ? , ?)"
, new String[] {title, content });
}
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);
}
@Override
public void onDestroy()
{
super.onDestroy();
// 退出程序时关闭SQLiteDatabase
if (db != null && db.isOpen())
{
db.close();
}
}
}
SQLiteDatabase需要想jdbc编程中那样关闭statement和connection连接,否则会引发资源泄露,总结起来,使用SQLiteDatabase进行数据库连接操作步骤如下:
1.获取SQLiteDatabase对象,他代表了与数据库的连接。
2.调用SQLiteDatabase的方法来执行sql语句。
3.操作sql语句的执行结果,比如用SimpleCursorAdapter来封装Cursor
4.关闭SQLi特DATabase,回收资源。
在Android SDK的platform-tools目录下提供了一个sqlite3.exe文件,他是一个简单的SQLite数据库的管理工具,类似于MySQL提供的命令行窗口。有些时候,开发者可以利用其来查询,管理数据库。常用命令如下:
.database:查出当前数据库
.tables:查出当前数据库里的数据表
.help:查出sqlite3支持的命令