《Android進階》Sqlite的使用

來源:互聯網
上載者:User

標籤:

之前認為Sqlite只能一次性建立多個表,其實不是

關鍵是對Sqlite的操作需要一些技巧:

package com.example.mydemo;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 MyDatabaseHelper extends SQLiteOpenHelper{    public static final String CREATE_BOOK = "create table book ("            + "id integer primary key autoincrement, "            + "author text, "            + "price real, "            + "pages integer, "            + "name text)";        public static final String CREATE_CATEGORY = "create table Category ("            + "id integer primary key autoincrement, "            + "category_name text, "            + "category_code integer)";        private Context mContext;        public MyDatabaseHelper(Context context, String name,            CursorFactory factory, int version) {        super(context, name, factory, version);        mContext = context;    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL(CREATE_BOOK);//        db.execSQL(CREATE_CATEGORY);    /*    db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)",                new String[] { "The Da Vinci Code", "Dan Brown", "454", "16.96" });*/        Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();            }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        // TODO Auto-generated method stub         switch(oldVersion){           case 3:               db.execSQL(CREATE_CATEGORY);           case 4:           case 5:           case 6:               db.execSQL("alter table Book add column category_idd integer");           default:                        }    }}

目前很好奇,怎麼得到資料庫的版本(當然db.getVersion())肯定可以

但如果僅僅是一個.db檔案,沒發現哪個視覺化檢視可以直接查看

 

package com.example.mydemo;import android.app.Activity;import android.app.Fragment;import android.content.ContentValues;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;/** * A placeholder fragment containing a simple view. */public class PlaceholderFragment extends Fragment {    OnDtaReceivedListener mCallback;    private MyDatabaseHelper dbHelper;    // Container Activity must implement this interface    public interface OnDtaReceivedListener {        public void onTextSelected(View view);    }    public PlaceholderFragment() {    }    @Override    public void onAttach(Activity activity) {        // TODO Auto-generated method stub        super.onAttach(activity);        try {            mCallback = (OnDtaReceivedListener) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString()                    + " must implement OnDtaReceivedListener");        }    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View rootView = inflater.inflate(R.layout.fragment_main, container,                false);        dbHelper = new MyDatabaseHelper(getActivity(), "BookStore.db", null, 7);        Button createDatabase = (Button) rootView                .findViewById(R.id.create_database);        createDatabase.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                dbHelper.getWritableDatabase();            }        });        Button addData = (Button) rootView.findViewById(R.id.add_data);        addData.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                SQLiteDatabase db = dbHelper.getWritableDatabase();                ContentValues values = new ContentValues();                // 開始組裝第一條資料                values.put("name", "The DaVinci Code");                values.put("author", "Dan Brown");                values.put("pages", 454);                values.put("price", 16.96);                db.insert("Book", null, values); // 插入第一條資料                values.clear();                // 開始組裝第二條資料                values.put("name", "The Lost Symbol");                values.put("author", "Dan Brown");                values.put("pages", 510);                values.put("price", 19.95);                db.insert("Book", null, values); // 插入第二條資料                values.clear();                // 開始組裝第三條資料  可以允許某條資料為空白                values.put("name", "The Lost1");                values.put("pages", 510);                values.put("price", 19.95);                db.insert("Book", null, values); // 插入第二條資料            }        });        Button updateData = (Button) rootView.findViewById(R.id.update_data);        updateData.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                SQLiteDatabase db = dbHelper.getWritableDatabase();                ContentValues values = new ContentValues();                values.put("price", 10.99);                db.update("Book", values, "name = ?",                        new String[] { "The DaVinci Code" });                                //上述代碼想表達的意圖就是,將名字是 The Da Vinci Code的這本書的價格改成 10.99            }        });                Button deleteButton = (Button) rootView.findViewById(R.id.delete_data);        deleteButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                SQLiteDatabase db = dbHelper.getWritableDatabase();                db.beginTransaction(); // 開啟事務                db.delete("book", "name = ?", new String[] { "The Da Vinci Code" });                db.delete("book", "id = ?", new String[] { "4" });            }                    });        return rootView;    }}

布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/create_database"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Create database" />    <Button        android:id="@+id/add_data"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Add data" />    <Button        android:id="@+id/update_data"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Update data" />    <Button        android:id="@+id/delete_data"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Delete data" /></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.