main代码
package com.example.app2;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button buttonInsertOne;
private Button buttonInsertTwo;
private Button buttonDeleteOne;
private Button buttonDeleteTwo;
private Button buttonUpdateOne;
private Button buttonUpdateTwo;
private Button buttonQueryOne;
private Button buttonQueryTwo;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
buttonInsertOne = (Button) findViewById(R.id.button_insert_one);
buttonInsertTwo = (Button) findViewById(R.id.button_insert_two);
buttonDeleteOne = (Button) findViewById(R.id.button_delete_one);
buttonDeleteTwo = (Button) findViewById(R.id.button_delete_two);
buttonUpdateOne = (Button) findViewById(R.id.button_update_one);
buttonUpdateTwo = (Button) findViewById(R.id.button_update_two);
buttonQueryOne = (Button) findViewById(R.id.button_query_one);
buttonQueryTwo = (Button) findViewById(R.id.button_query_two);
MySqlHelp mySqlHelp=new MySqlHelp(this,"student.db",null,1);
db = mySqlHelp.getReadableDatabase();
buttonInsertOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues = new ContentValues();
contentValues.put("name","小蘑菇");
contentValues.put("sex","男");
db.insert("student",null,contentValues);
}
});
buttonInsertTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sql="insert into student values(null,?,?)";
db.execSQL(sql,new Object[]{"小明","男"});
}
});
buttonQueryOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sql="select * from student";
Cursor cursor = db.rawQuery(sql, null);
if (cursor!=null){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(MainActivity.this, "id:"+id+"--姓名:"+name+"--性别:"+sex, Toast.LENGTH_SHORT).show();
}
}
cursor.close();
}
});
buttonQueryTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.query("student", null, null, null, null, null, null);
if (cursor!=null){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(MainActivity.this, "2-->id:"+id+"--姓名:"+name+"--性别:"+sex, Toast.LENGTH_SHORT).show();
}
}
cursor.close();
}
});
buttonDeleteOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.delete("student","id=?",new String[]{"11"});
}
});
buttonDeleteTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sql="delete from student where name=?";
db.execSQL(sql,new Object[]{"蘑菇3"});
}
});
buttonUpdateOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues = new ContentValues();
contentValues.put("name","可爱的蘑菇");
db.update("student",contentValues,"id=?",new String[]{"2"});
}
});
buttonUpdateTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sql="update student set name=? where id=?";
db.execSQL(sql,new Object[]{"蛋白",6});
}
});
}
}
mySqlHelp
package com.example.app2;
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) {
db.execSQL("create table student(id integer primary key autoincrement,name varcher(20),sex varcher(10))");
db.beginTransaction();
for (int i = 0; i < 10; i++) {
String sql="insert into student values(null,?,?)";
db.execSQL(sql,new String[]{"蘑菇"+i,"女"});
}
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}