SQLite 是android应用程序自带的一个数据库,很小,很快。
下面对他的使用简单的介绍一下。
最好可以使用adb shell去查看,有点要注意的是,如果用真机模拟的时候,需要开启root权限,不然是看不了databases的内容的。虚拟机的话可以直接查看。
布局文件xml
<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=".SQLiteTestActivity" >
<Button
android:id="@+id/BTN_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create DataBase"/>
<Button
android:id="@+id/BTNINS_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert DataBase"/>
<Button
android:id="@+id/BTNQUR_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Query DataBase"/>
</LinearLayout>
然后是一个继承了SQLiteOpenHelper的自定义类package com.example.sqlitetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseHepler extends SQLiteOpenHelper{
public static final String CreateStr = "create table Book("
+"id integer primary key autoincrement, "
+"author text, "
+"price real, "
+"pages integer, "
+"name text)" ;
private Context mcontext ;
public MyDatabaseHepler(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
mcontext = context ;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(CreateStr) ;
Toast.makeText(mcontext, "Create succsse", Toast.LENGTH_LONG).show() ;
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
activty.java文件
package com.example.sqlitetest;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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;
import android.widget.Toast;
public class SQLiteTestActivity extends Activity {
private MyDatabaseHepler myDatabaseHepler ;
private Button btn ;
private Button btnInset ;
private Button btnQuery ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_test);
myDatabaseHepler = new MyDatabaseHepler(this, "NewBookStore.db", null, 1) ;
/*在点击btn响应了对应的代码 MyDtabaseHelper构造的时候传入的第二个参数,是一个数据库 而不是一个表
*当myDatabaseHelper去调用getWriterDatabase的时候会发现当前数据库并没有这个数据库
*于是就会去响应 oncreat的方法于是 BookStore就创建了 并且在Oncreate里面 也实现了创建表book的方法
*下次在点击的时候 就知道数据库已经存在BookStroe了 那么oncreate就不会再去调用 */
btn = (Button) findViewById(R.id.BTN_ID) ;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myDatabaseHepler.getWritableDatabase() ;
}
}) ;
btnInset = (Button) findViewById(R.id.BTNINS_ID) ;
btnInset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db = myDatabaseHepler.getWritableDatabase() ;
ContentValues values= new ContentValues() ;
values.put("name","nihao") ;
values.put("author","buhao") ;
values.put("pages",12) ;
values.put("price",1.2) ;
db.insert("Book", null, values) ;
Log.d("Insert11","1") ;
}
}) ;
btnQuery = (Button) findViewById(R.id.BTNQUR_ID) ;
btnQuery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db = myDatabaseHepler.getWritableDatabase() ;
Cursor cursor = db.query("Book",null,null,null,null,null,null) ;
if(cursor.moveToFirst()){
do {
String name = cursor.getString(cursor.getColumnIndex("name") ) ;
Log.d("BookName",name) ;
} while (cursor.moveToNext());
}
cursor.close() ;
}
}) ;
}
@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_test, menu);
return true;
}
}
本文介绍如何在Android应用中使用SQLite数据库进行基本操作,包括创建数据库、插入数据和查询数据。通过实现一个自定义类和相关活动,展示了如何在Android环境中有效地管理和利用SQLite数据库。
9万+

被折叠的 条评论
为什么被折叠?



