项目短视频
操作示范如下(时长26:16)

【例5-5】 创建与删除数据库
编写一个能反复创建与删除数据库的演示程序,数据库命名为eBook.db
布局文件activity_db.xml的源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="100dp"
>
<Button
android:id="@+id/createdb"
android:layout_width="178dp"
android:layout_height="40dp"
android:text="重建数据库"
android:textSize="18sp" />
<Button
android:id="@+id/deletedb"
android:layout_width="178dp"
android:layout_height="40dp"
android:text="删除数据库"
android:textSize="18sp" />
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="数据库测试"
android:textSize="24sp" />
</LinearLayout>
控制文件DBActivity.java源代码
package com.example.chap05;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DBActivity extends AppCompatActivity {
Button createdb,deletedb;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);
createdb = (Button)findViewById(R.id.createdb);
deletedb = (Button)findViewById(R.id.deletedb);
txt = (TextView)findViewById(R.id.txt);
createdb.setOnClickListener(new mClick());
deletedb.setOnClickListener(new mClick());
}
class mClick implements View.OnClickListener {
@Override
public void onClick(View view) {
if(view==createdb){
MySQLDatabase mdb= new MySQLDatabase();
Toast.makeText(DBActivity.this, "数据库和数据表创建成功", Toast.LENGTH_SHORT).show();
}else if(view==deletedb){
deleteDatabase(MySQLDatabase.Database_name);
txt.setText("删除数据库成功");
Toast.makeText(DBActivity.this, "数据库和数据表删除成功", Toast.LENGTH_SHORT).show();
}
}
}
class MySQLDatabase{
private static final String Database_name = "eBook.db";//数据库名
private String TABLE_NAME = "diary"; //数据表名
private String ID = "nid"; //ID编号
private String TITLE = "title"; //标题
private String BODY = "body"; //正文
private MySQLDatabase(){
try {
int mode = Context.MODE_PRIVATE;
//创建数据库
SQLiteDatabase db = openOrCreateDatabase(Database_name, mode, null);
//SQL语句
String DATABASE_CREATE="CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
ID + " INTEGER primary key autoincrement," +
TITLE + " text not null, " +
BODY + " text not null);";
db.execSQL(DATABASE_CREATE);//创建数据表
txt.setText("创建数据库成功");
}catch (SQLException e){
e.printStackTrace();
txt.setText("创建数据库失败");
}
}
}
}
打开项目配置文件AndroidManifest.xml,修改工程要启动的Activity类名
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chap05">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DBActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

