Android 資料庫SQLITE(SQLITEOPENHELPER運用)

來源:互聯網
上載者:User

 
---------------------------------------------.java
package com.example.sqlite;
 
 
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
 
public class MainActivity extends
Activity {
Button mButton1, mButton2,
mButton3;
MyDbHelper dbhelper;
SQLiteDatabase sqLiteDatabase;
 
 
private static final int old_VERSION = 1;
private static final int new_VERSION = 2;
 
 
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
 
 
mButton1.setOnClickListener(new OnClickListener() {
 
 
public void onClick(View v) {
// sqLiteDatabase = openOrCreateDatabase(
// DBNAME,
// Context.MODE_PRIVATE,
// null);
//
//
// if (sqLiteDatabase!=null) {
//
// Toast.makeText(getBaseContext(), "建立成功"+DBNAME, 2000).show();
// }
// else {
// Toast.makeText(getBaseContext(), "建立失敗"+DBNAME, 2000).show();
// }
//
 
dbhelper=new MyDbHelper(getBaseContext(), old_VERSION);
 
sqLiteDatabase=dbhelper.getWritableDatabase();
 
Toast.makeText(getBaseContext(), "建立成功", 2000).show();
}
});
 
mButton2.setOnClickListener(new OnClickListener() {
 
public void onClick(View v) {
// TODO Auto-generated method stub
// sqLiteDatabase.close();
 
 
dbhelper=new MyDbHelper(getBaseContext(), new_VERSION);
sqLiteDatabase=dbhelper.getWritableDatabase();
 
Toast.makeText(getBaseContext(), "成功更新", 2000).show();
}
});
 
// mButton3.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
//
// boolean isDeleted=deleteDatabase(DBNAME);
//
// if (isDeleted) {
//
//
//
// Toast.makeText(getBaseContext(), "成功刪除資料庫"+DBNAME, 2000).show();
// }
// else {
// Toast.makeText(getBaseContext(), "刪除資料庫失敗"+DBNAME, 2000).show();
// }
// }
// });
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
dbhelper.close();
}
 
 
@Override
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}
 
 
---------------------------------MyDbHelper.java
package com.example.sqlite;
 
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
 
public class MyDbHelper extends SQLiteOpenHelper{
 
 
private static final String DB_NAME = "db_2.db3";
private static final String TAG = "LOOK";
public MyDbHelper(Context context,
 
int version) {
super(context, DB_NAME, null, version);
// TODO Auto-generated constructor stub
}
 
 
@Override
public void onCreate(
SQLiteDatabase db) {
// TODO Auto-generated method stu"
Log.d(TAG, "Begin execute  onCreate");
 
try {
db.beginTransaction();
String sqlString="CREATE TABLE TBL_USER(_id INTEGER PRIMARY KEY,name TEXT NOT NULL)";
db.execSQL(sqlString);
 
 
db.setTransactionSuccessful();
 
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
db.endTransaction();}
 
Log.d(TAG, " execute  onCreate Completed.");
}
 
 
@Override
public void onUpgrade(
SQLiteDatabase db,
int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
 
 
Log.d(TAG, "Begin execute  onUpgrade");
try {
db.beginTransaction();
 
String upgradeString="ALTER TABLE TBL_USER ADD COLUMN created_dt timestamp NOT NULL DEFAULT '2012-09-07' COLLATE NOCASE;";
db.execSQL(upgradeString);
db.setTransactionSuccessful();
}
catch (Exception e) {
// TODO: handle exception
 
e.printStackTrace();
}
finally{
db.endTransaction();
}
 
 
Log.d(TAG, " execute  onUpgrade Completed");
 
}
 
 

聯繫我們

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