Android編程之資料庫Sql編程執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程之資料庫Sql編程實現方法。分享給大家供大家參考。具體分析如下:

Android中安裝輕量級資料庫Sqlite,現在測試資料庫基本操作。

資料庫基本操作:建立表,插入,刪除可以用execSQL();讀取可以用rawQuery();這兩個函數都可以標準SQL語句進行操作。

原始碼:

package com.test.sql; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; public class test_sql extends Activity {  DatabaseHelper mOpenHelper;  private static final String DATABASE_NAME = "test.db";  private static final int DATABASE_VERSION = 1;  private static class DatabaseHelper extends SQLiteOpenHelper {   DatabaseHelper(Context context) {    super(context, DATABASE_NAME, null, DATABASE_VERSION);   }   @Override   public void onCreate(SQLiteDatabase db)   {    //建立一個使用者表    //共有5項:Id Pwd Name OnLineTime Level    db.execSQL("CREATE TABLE User_Table ('Id' INT,'Pwd' VARCHAR,'Name' VARCHAR,'OnLineTime' INT,'Level' INT)");  }   @Override   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  }  }  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);  setContentView(R.layout.main);  mOpenHelper = new DatabaseHelper(this);  SQLiteDatabase db = mOpenHelper.getWritableDatabase();  //清空資料   db.execSQL("DELETE FROM User_Table");   //插入資料10000 123456 "jdh" 0 0   //插入資料10001 123456 "jim" 0 0   db.execSQL("INSERT INTO User_Table VALUES (?,?,?,?,?)", new Object[]{10000,"123456","jdh",0,0});   db.execSQL("INSERT INTO User_Table VALUES (?,?,?,?,?)", new Object[]{10001,"123456","jim",0,0});   //讀取資料   Cursor cursor = db.rawQuery("SELECT * FROM User_Table WHERE Name = ?",new String[]{"jdh"});   while (cursor.moveToNext())   {    String str = cursor.getInt(0) + cursor.getString(1) + cursor.getString(2) + cursor.getInt(3) + cursor.getInt(4);    Log.i("str:", str);   }   } }

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.