300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android studio 83 数据库插入 查询 更新 删除 sqlite

Android studio 83 数据库插入 查询 更新 删除 sqlite

时间:2020-10-01 12:47:49

相关推荐

Android studio 83 数据库插入 查询 更新 删除 sqlite

activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名"android:textSize="30sp" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入姓名"android:textSize="20sp"android:id="@+id/name" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="年龄"android:textSize="30sp" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入年龄"android:textSize="20sp"android:id="@+id/age" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="插入"android:id="@+id/btn_insert"android:textAllCaps="false" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="更新"android:id="@+id/btn_update"android:textAllCaps="false" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="查询"android:id="@+id/btn_search"android:textAllCaps="false" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="20sp"android:text="删除"android:id="@+id/btn_delete"android:textAllCaps="false" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/tv_show"android:textSize="20sp"android:gravity="center_horizontal"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/tv_showAge"android:textSize="20sp"android:gravity="center_horizontal" /></LinearLayout></LinearLayout>DatabaseHelper.javapackage com.example.mysqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DatabaseHelper extends SQLiteOpenHelper {public DatabaseHelper(Context context){super(context,"Test.db",null,1);}//第一个参数是上下文,第二个参数是数据库名称,//第三个参数是CursorFactory对象,一般设置为null,第四个参数是数据库的版本public void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)");//创建表单***********(*** 整型 自增主键,name 字符(20),age 整型)}//创建表 表名information 表结构 自增id,字符串姓名,int年龄//数据库版本发生变化时调用public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.d("myDeBug","数据库版本已更新");}}MainActivity.javapackage com.example.mysqlite;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.EditText;import android.widget.TextView;public class MainActivity extends AppCompatActivity {private Button insertButton,updateButton,searchButton,deleteButton;//插入、更新、查询、删除private EditText name,age;//名字、年龄private TextView show,showAge;//显示名字、年龄final DatabaseHelper dbHelper=new DatabaseHelper(MainActivity.this);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);insertButton=findViewById(R.id.btn_insert);//插入、更新、查询、删除updateButton=findViewById(R.id.btn_update);searchButton=findViewById(R.id.btn_search);deleteButton=findViewById(R.id.btn_delete);name=findViewById(R.id.name);//名字age=findViewById(R.id.age);//年龄show=findViewById(R.id.tv_show);//显示名字showAge=findViewById(R.id.tv_showAge);//显示年龄SQLiteDatabase db=dbHelper.getReadableDatabase();myShow();insertButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name",name.getText().toString());values.put("age",age.getText().toString());long id =db.insert("information",null,values);Log.d("myDeBug","insert");myShow();db.close();name.setText(null);age.setText(null);}});updateButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();//获取一个用于操作数据库的SQLiteDatabase实例ContentValues values=new ContentValues();values.put("age",age.getText().toString());db.update("information",values,"name=?",new String[]{name.getText().toString()});myShow();db.close();Log.d("myDebug","update");name.setText(null);age.setText(null);}});searchButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();String name1=name.getText().toString();show.setText(null);if(name1.equals("")){//show.setText("姓名");//showAge.setText("年龄");//Cursor cursor = db.rawQuery("select * from information",null);//while (cursor.moveToNext()) {// String newName = cursor.getString(cursor.getColumnIndex("name"));// int newAge = cursor.getInt(cursor.getColumnIndex("age"));// show.setText(show.getText() + "\n" + newName);// showAge.setText(showAge.getText()+"\n" + newAge);//}myShow();db.close();}else {show.setText("姓名");showAge.setText("年龄");Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});while (cursor.moveToNext()) {String newName = cursor.getString(cursor.getColumnIndex("name"));int newAge = cursor.getInt(cursor.getColumnIndex("age"));// show.setText(show.getText() + "\n" + newName + "\t" + newAge);show.setText(show.getText() + "\n" + newName);showAge.setText(showAge.getText()+"\n" + newAge);}cursor.close();db.close();name.setText(null);age.setText(null);}}});deleteButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db=dbHelper.getWritableDatabase();db.delete("information","name=?",new String[]{name.getText().toString()});myShow();db.close();Log.d("myDeBug","DeleteSuccess");name.setText(null);age.setText(null);}});}public void myShow(){SQLiteDatabase db=dbHelper.getReadableDatabase();show.setText("姓名");showAge.setText("年龄");Cursor cursor = db.rawQuery("select * from information",null);//查询所有数据while (cursor.moveToNext()) {String newName = cursor.getString(cursor.getColumnIndex("name"));//查询数据nameint newAge = cursor.getInt(cursor.getColumnIndex("age"));//查询数据ageshow.setText(show.getText() + "\n" + newName);//在一个textView显示所有查询到的数据一条加一个换号showAge.setText(showAge.getText()+"\n" + newAge);}cursor.close();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。