Android學習筆記——SQLite

來源:互聯網
上載者:User

標籤:

該工程的功能是實現關於資料庫的操作,即creat、update、insert、query、delete

調試的時候請用模擬器,用真機調試的時候進入cmd-adb shell,再進入cd data/data的時候會顯示permission denied

 

以下的代碼是MainActivity.java中的代碼

package com.example.sqlite;import com.example.sqlite.db.DataBaseHelper;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {        private Button createDBButton ;    private Button insertDBButton ;    private Button updateDBButton ;    private Button queryDBButton ;    private Button deleteDBButton ;    private EditText input_id ;    private EditText input_name ;    private TextView test ;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //此處為建立資料庫, 通過按鈕監聽器來實現        createDBButton = (Button) findViewById(R.id.creatDB);        createDBButton.setOnClickListener(new CreateDBListener());        //此處為插入資料到資料庫中        insertDBButton = (Button) findViewById(R.id.insert);        insertDBButton.setOnClickListener(new InsertListener());        //此處為更新資料表        updateDBButton = (Button) findViewById(R.id.update);        updateDBButton.setOnClickListener(new updateListener());        //此處為更新資料表        queryDBButton = (Button) findViewById(R.id.query);        queryDBButton.setOnClickListener(new queryListener());        //此處為刪除資料內容        deleteDBButton = (Button) findViewById(R.id.delete);        deleteDBButton.setOnClickListener(new deleteListener());        //此處為顯示查詢結果        test = (TextView) findViewById(R.id.result);        test.setText("name") ;        //此處為添加資料的選項        //input_id = (EditText) findViewById(R.id.input_id);        //input_name = (EditText) findViewById(R.id.input_name);    }        class CreateDBListener implements OnClickListener{        @Override        public void onClick(View v) {            // 此處為調用另外一個類中的方法來建立資料庫, 或者直接來建立資料庫            String db_name = "test_mars_db_one" ;            System.out.println("Create");            DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this, db_name) ;            SQLiteDatabase db = dbHelper.getReadableDatabase() ;        }     }        class InsertListener implements OnClickListener{        @Override        public void onClick(View v) {            //產生一個ContentValues對象            ContentValues values = new ContentValues() ;            //想該對象當中插入索引值對,其中鍵是列名,值是希望插入到這列的值,值必須            values.put("id", 1) ;            values.put("name", "zhangsan") ;            System.out.println("Insert");            DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this, "test_mars_db_one") ;            SQLiteDatabase db = dbHelper.getWritableDatabase() ;            //調用insert方法, 就可以將資料插入到資料庫中            db.insert("user", null, values) ;        }            }    class updateListener implements OnClickListener{        @Override        public void onClick(View arg0) {            // 此處為更新資料內容               System.out.println("Update");            DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this, "test_mars_db_one") ;            SQLiteDatabase db = dbHelper.getWritableDatabase() ;            ContentValues values = new ContentValues() ;            values.put("name", "zhangsanfeng") ;            //第一個參數為要更新的表名            //第二個參數為一個ContentValues對象            //第三個參數是where語句            db.update("user", values, "id=?", new String[]{"1"}) ;        }    }            class queryListener implements OnClickListener{        @Override        public void onClick(View v) {            // 此處為查詢資料內容, 並用到cursor來實現            System.out.println("query");            DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this, "test_mars_db_one") ;            SQLiteDatabase db = dbHelper.getWritableDatabase() ;            Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"2"},null,null,null,null) ;            while(cursor.moveToNext()){                String name = cursor.getString(cursor.getColumnIndex("name")) ;                System.out.print("query---> " + name) ;                //name += name ;                test.setText(name) ;            }        }    }        class deleteListener implements OnClickListener{        public void onClick(View v) {         //此處為實現刪除資料            System.out.println("delete");            DataBaseHelper dbHelper = new DataBaseHelper(MainActivity.this, "test_mars_db_one") ;            SQLiteDatabase db = dbHelper.getWritableDatabase() ;            //刪除特定條件的資料            //db.delete("user","id=?",new String[]{"2"});            //刪除所有資料            db.delete("user",null,null);        }    }}            

 

以下的代碼是DataBaseHelper.java中的代碼

package com.example.sqlite.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;//DatabaseHelper作為一個訪問SQLite的助手類,提供兩個方面的功能//第一,getReadableDatabase(),getWriteableDatabase()可以獲得SQLiteDatabase對象,通過該對象可以對資料庫進行操作//第二,提供了onCreate()和onUpgrade()兩個回呼函數,允許我們在建立和升級資料庫時,進行自己的操作public class DataBaseHelper extends SQLiteOpenHelper {        private static final int VERSION = 1;    //在SQLiteOpenHelper的子類當中,必須有該建構函式    public DataBaseHelper(Context context, String name, CursorFactory factory,            int version) {        //必須通過super調用父類當中的建構函式        super(context, name, factory, version);        // TODO Auto-generated constructor stub    }        public DataBaseHelper(Context context,String name){        this(context,name,VERSION);    }        public DataBaseHelper(Context context,String name,int version){        this(context,name,null,version);    }        //該函數是在第一次建立資料庫的時候執行,實際上是在第一次得到SQLiteDatabase對象的時候,才會調用該方法    @Override    public void onCreate(SQLiteDatabase db) {        // TODO Auto-generated method stub        System.out.println("Create a Database");        //execSQL函數用於執行SQL語句        db.execSQL("create table user(id int,name varchar(20))");        System.out.println("Create a Database successful");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        // TODO Auto-generated method stub        System.out.println("update a DataBase");    }}

 

以下的代碼是activity_main.xml中的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >            <Button        android:id="@+id/creatDB"        android:text="creatDatabase"        android:layout_width="fill_parent"        android:layout_height="wrap_content"            />        <Button        android:id="@+id/update"        android:text="updateDatabase"        android:layout_width="fill_parent"        android:layout_height="wrap_content"            />        <Button        android:id="@+id/insert"        android:text="Insert"        android:layout_width="fill_parent"        android:layout_height="wrap_content"            />        <Button        android:id="@+id/query"        android:text="query"        android:layout_width="fill_parent"        android:layout_height="wrap_content"            />        <Button        android:id="@+id/delete"        android:text="delete"        android:layout_width="fill_parent"        android:layout_height="wrap_content"            />            <TextView        android:id="@+id/result"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></LinearLayout>

 

Android學習筆記——SQLite

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.