SQLite数据库操作 (原始操作)

时间:2022-06-17 05:05:11

android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,

使用该类可以完成对数据进行添加(Create)、查询(Retrieve)、更新(Update)和删除(Delete)操作(这些操作简称为CRUD)。

对SQLiteDatabase的学习,我们应该重点掌握execSQL()和rawQuery()方法

execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL语句;

rawQuery()方法可以执行select语句。

参考 http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6995

官方 http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

操作数据库步骤:

一、获取代表数据库的SQLiteDatabase对象

SQLiteDatabase 提供静态方法获取SQLiteDatabase对象

  SQLite数据库操作 (原始操作)

二、 对数据库CRUD操作

   void execSQL(String sql, Object[] bindArgs )

      官方解释: 

      Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.  

     For example, the following are good candidates for using this method:

    • ALTER TABLE
    • CREATE or DROP table / trigger / view / index / virtual table
    • REINDEX
    • RELEASE
    • SAVEPOINT
    • PRAGMA that returns no data

        

    Cursor rawQuery(Stringsql, String[] selectionArgs)方法可以执行select语句。

废话说了那么多了,咋们举个栗子吧:

  下面是布局文件,两个EditText , 用于插入数据库的name 和 content 字段的值。一个 TextView 用于显示从数据库查询的数据。

<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=".MainActivity" > <EditText
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/edt_name" />
<EditText
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/edt_content" />
<TextView
android:text=""
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <Button
android:onClick="insert"
android:id="@+id/btn_insert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="click insert"
/>
<Button
android:onClick="show"
android:id="@+id/btn_show"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="click show"
/> </LinearLayout>

  

再加个界面吧, 一个Activity :

  

public class MainActivity extends Activity {

	String tag="MainActivity";
SQLiteDatabase db;
EditText edt_name ,edt_content;
TextView tv_name; public void createDB(){
// 捕获异常是因为db.execSQL(sql)方法,如果表已经存在会报异常。
try{ db=SQLiteDatabase.openOrCreateDatabase(getFilesDir().toString()+"test.db", null);
//创建表的sql语句
String sql="create table user(_id integer primary key autoincrement,name varchar(50) ,content varchar(250))";
db.execSQL(sql);
Log.i(tag, "--------createDB()-------"); }catch(Exception e){} }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_name=(EditText) findViewById(R.id.edt_name);
edt_content=(EditText) findViewById(R.id.edt_content);
tv_name =(TextView) findViewById(R.id.tv_name); this.createDB();
}
public void insert(View v){ String sql="insert into user values(null,?,?)";
String name =edt_name.getText().toString();
String content =edt_content.getText().toString();
String[] bindArgs=new String[]{name,content}; db.execSQL(sql, bindArgs);
Log.i(tag, "--------updata()-------"); }
public void show(View v){
String sql="select * from user";
Cursor cursor = db.rawQuery(sql,null);
while(cursor.moveToNext()){
// 将数据全部显示在TextView中, 很不规范、阅读性很差的代码!!!
tv_name.setText(tv_name.getText().toString()+"\n id="+cursor.getString(0)+" name="+cursor.getString(1)+ " content="+cursor.getString(2)); }
Log.i(tag, "--------show()-------");
} }

  

遇到的问题:

  1、sql语句出错!

  2、sql语句又出错!

  3、重复创建表

  SQLite数据库操作 (原始操作)

  以上就是原始的SQLiteDatabase操作:

        一、 没有insert() 、 updata() 、delete() 、query() 等操作,因为 execSQL() 是万能的!

        二、 没有使用SQLiteOpenHelper类 ( 但此类能简化我们对数据库的操作,在下一篇介绍)

  补充:Cursor、 SQLiteDatabase 用完之后要close掉

一个很详细的教程:http://www.eoeandroid.com/thread-182378-1-1.html