1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package com.example.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast;
import DB.DatabaseHelper;
public class MainActivity extends AppCompatActivity { SQLiteDatabase db1,db2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1=(Button)this.findViewById(R.id.bt1); Button bt2=(Button)this.findViewById(R.id.bt2); Button bt3=(Button)this.findViewById(R.id.bt3); Button bt4=(Button)this.findViewById(R.id.bt4); Button bt5=(Button)this.findViewById(R.id.bt5); Button bt6=(Button)this.findViewById(R.id.bt6); }
public void db1_create(View v){ DatabaseHelper dbHelper = new DatabaseHelper(this, "db1.db", null, 1); db1=dbHelper.getWritableDatabase(); Log.e("db1","create success"); } public void db2_create(View v){ db2 = SQLiteDatabase.openOrCreateDatabase ("/data/data/com.example.sqlite/databases/db2.db",null); String createtableSql = " create table user(id integer(10) , name varchar(10),primary key(id))"; db2.execSQL(createtableSql); Toast.makeText(this, "数据库 db2 创建成功", Toast.LENGTH_SHORT).show(); Log.e("db2","create success"); } public void insert(View v){ ContentValues values=new ContentValues(); values.put("id", "0"); values.put("name", "zs"); db1.insert("user", null, values); values.put("id", "1"); values.put("name", "ls"); db1.insert("user", null, values); values.put("id", "2"); values.put("name", "ww"); db1.insert("user", null, values); Toast.makeText(this, "数据库 db1 插入数据成功", Toast.LENGTH_SHORT).show();
String insertSql ="INSERT INTO user(id,name)VALUES(0,'zs'),(1,'ls'),(2,'ww')"; db2.execSQL(insertSql); Toast.makeText(this, "数据库 db2 插入数据成功", Toast.LENGTH_SHORT).show(); } public void delete(View v){ String whereClause = "name=?"; String[] whereArgs = {"zs"}; db1.delete("user",whereClause,whereArgs); Toast.makeText(this, "数据库 db1 删除zs成功", Toast.LENGTH_SHORT).show();
String deleteSql ="DELETE FROM user WHERE name='zs'"; db2.execSQL(deleteSql); Toast.makeText(this, "数据库 db2 删除zs成功", Toast.LENGTH_SHORT).show(); } public void update(View v){ ContentValues values = new ContentValues(); values.put("name", "zs"); String whereClause = "name=?"; String[] whereArgs = {"ls"}; db1.update("user",values,whereClause,whereArgs); Toast.makeText(this, "db1 成功将ls改为zs", Toast.LENGTH_SHORT).show();
String updateSql ="UPDATE user SET name='zs' WHERE name='ls';"; db2.execSQL(updateSql); Toast.makeText(this, "db2 成功将ls改为zs", Toast.LENGTH_SHORT).show();
} public void query(View v){ String querySQl="select * from user"; Cursor cursor=db1.rawQuery(querySQl, null); while (cursor.moveToNext()) { int id = cursor.getInt(0); //获取第一列的值,第一列的索引从0开始 String name = cursor.getString(1);//获取第二列的值 Log.e("data",id+":"+name); } Toast.makeText(this, "查询完成", Toast.LENGTH_SHORT).show(); } }
|