Android學習筆記-SQLite的使用

來源:互聯網
上載者:User

標籤:android

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/53/9E/wKioL1RsXxnz29SwAAC3SlphABw567.jpg" title="QQ20141119171030.png" alt="wKioL1RsXxnz29SwAAC3SlphABw567.jpg" />

介面檔案activity_main.xml

    <Button         android:id="@+id/createButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/hello_world"        android:text="create database"/>    <Button         android:id="@+id/updateButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/createButton"        android:text="update database"/>        <Button         android:id="@+id/insertButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/updateButton"        android:text="insert database"/>        <Button         android:id="@+id/updateRecordButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/insertButton"        android:text="update record"/>        <Button         android:id="@+id/queryRecordButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/updateRecordButton"        android:text="Query record"/>

DatabaseHelper.java

package com.example.sqlite_01;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends SQLiteOpenHelper{private static final int VERSION = 1;public DatabaseHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}public DatabaseHelper(Context context, String name, int  version) {this(context, name, null, version);}public DatabaseHelper(Context context, String name) {this(context, name, VERSION);}//第一次建立資料庫的時候執行,實際上是在第一次得到SQLiteDatabase對象的時候調用@Overridepublic void onCreate(SQLiteDatabase db) {System.out.println("Create a database");db.execSQL("create table user(id int, name varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("Update a database");}/* * abd shell進入當前App的目錄下的databases目錄下就可以看到建立的資料庫 * 開啟資料庫sqlite3 test_db * 查看當前資料庫裡的表以及建立表的語句 .schema */}

MainActivity.java

package com.example.sqlite_01;import android.support.v7.app.ActionBarActivity;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;public class MainActivity extends ActionBarActivity {private Button createButton = null;private Button updateButton = null;private Button insertButton = null;private Button updateRecordButton = null;private Button queryRecordButton = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);createButton = (Button) findViewById(R.id.createButton);updateButton = (Button) findViewById(R.id.updateButton);insertButton = (Button) findViewById(R.id.insertButton);updateRecordButton = (Button) findViewById(R.id.updateRecordButton);queryRecordButton = (Button) findViewById(R.id.queryRecordButton);createButton.setOnClickListener(new CreateListener());updateButton.setOnClickListener(new UpdateListener());insertButton.setOnClickListener(new InsertListener());updateRecordButton.setOnClickListener(new UpdateRecordListener());queryRecordButton.setOnClickListener(new QueryListener());}class CreateListener implements OnClickListener{@Overridepublic void onClick(View v) {DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db");SQLiteDatabase db = dbHelper.getReadableDatabase();//執行了這一句才會建立資料庫}}class UpdateListener implements OnClickListener{@Overridepublic void onClick(View v) {DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2);SQLiteDatabase db = dbHelper.getReadableDatabase();//執行了這一句才會建立資料庫}}class InsertListener implements OnClickListener{@Overridepublic void onClick(View v) {ContentValues contentValues = new ContentValues();contentValues.put("id", 1);contentValues.put("name", "umgsai");DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2);SQLiteDatabase db = dbHelper.getWritableDatabase();db.insert("user", null, contentValues);}}class UpdateRecordListener implements OnClickListener{@Overridepublic void onClick(View v) {DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2);SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", "admin");db.update("user", values, "id = ?", new String[]{"1"});}}class QueryListener implements OnClickListener{@Overridepublic void onClick(View v) {DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_db", 2);SQLiteDatabase db = dbHelper.getReadableDatabase();//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)Cursor cursor = db.query("user", new String[]{"id", "name"}, "id = ?", new String[]{"1"}, null, null, "id");while (cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));System.out.println("name>>>" + name);}}}}

可以通過adb shell命令來查看資料庫

>abd shell    

>ls -l        (查看目前的目錄下的檔案及檔案夾)

>cd data  (進入data目錄)

>ls -l        (查看目前的目錄下的檔案及檔案夾)

>cd data  (進入data目錄)

>ls -l        (查看目前的目錄下的檔案及檔案夾)

>cd com.example.sqlite_01  (進入當前項目目錄)

>ls -l        (查看目前的目錄下的檔案及檔案夾)

>cd databases  (進入databases目錄)

>ls -l        (此時就可以看到資料庫檔案test_db)

>sqlite3 test_db  (使用sqlite開啟test_db資料庫)

>.schema  (查看當前資料庫中的表以及建表的語句)

>select * from user;  (從user表中查詢資料)

本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1579729

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.