Android studio的數據庫的建立以及增刪改查
MainActivity
package com.example.dday09;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase readableDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//至關於創建鏈接
MySqlhelp mySqlhelp = new MySqlhelp(this,"user.db",null,1);
//打開 至關於數據庫建立完成
readableDatabase = mySqlhelp.getReadableDatabase();
}
// public void btn1(View view) {
//
MySqlhelp mySqlhelp = new MySqlhelp(this,"user.db",null,1);
//
//
//
// }
//建立表
public void createTable(View view) {
String sql = "create table student(id integer primary key autoincrement,name varchar(20))";
readableDatabase.execSQL(sql);
}
public void insert(View view) {
//插入數據
// String sql = "insert into student values(null,'xiaoming')";
// readableDatabase.execSQL(sql);
ContentValues contentValues = new ContentValues();
contentValues.put("name","嘻嘻");
readableDatabase.insert("student",null,contentValues);
}
public void query(View view) {
//查詢數據
// String sql = "select * from student where id = ?";
// Cursor cursor = readableDatabase.rawQuery(sql, new String[]{"3"});
Cursor cursor = readableDatabase.query("student", null, null, null, null, null, null);
if (cursor!=null){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(this, ""+name, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
}
public void delete(View view) {
// String sql = "delete from student where id = ?";
// readableDatabase.execSQL(sql,new Object[]{2});
readableDatabase.delete("student","id = ?",new String[]{ "3"});
}
public void update(View view) {
// String sql = "update student set name = '李白' where id = ?";
// readableDatabase.execSQL(sql,new Object[]{3});
ContentValues contentValues = new ContentValues();
contentValues.put("name","大白");
readableDatabase.update("student",contentValues,"id = ?",new String[]{ "3"});
}
}
MySqlhelp
package com.example.dday09;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySqlhelp extends SQLiteOpenHelper {
public MySqlhelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_main.xml(單純是一堆按鈕)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:text="建立數據庫"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:onClick="createTable"
android:text="建立表"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:onClick="insert"
android:text="insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:onClick="query"
android:text="query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:onClick="delete"
android:text="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:onClick="update"
android:text="update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>