当前位置:数据库 > SQLite >>

Android 数据库SQLITE(SQLITEOPENHELPER运用)

\

 
---------------------------------------------.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");
 
}
 
 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,