- 爱易网页
-
数据库教程
- Android 创办数据库(使用SqliteDataBase)
日期:2014-05-16 浏览次数:20369 次
Android 创建数据库(使用SqliteDataBase)
package com.example.sqlite;
import android.app.Activity;
import android.content.Context;
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;
SQLiteDatabase sqLiteDatabase;
private static final String DBNAME = "db_1";
@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();
}
}
});
mButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
sqLiteDatabase.close();
Toast.makeText(getBaseContext(), "关闭数据库"+DBNAME, 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();
}