我们的android布局页面:
布局部分的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<!--使用的就是我们的啊相对布局的方式来布局页面-->
<EditText
android:id="@+id/edit1"
android:hint="请输入你要插入的数据"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--接下来是跟新前后的数据的编写-->
<EditText
android:layout_below="@+id/edit1"
android:id="@+id/edit2left"
android:hint="更新前的数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_below="@+id/edit1"
android:layout_toRightOf="@id/edit2left"
android:id="@+id/edit2right"
android:hint="更新后的数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/del"
android:layout_width="236dp"
android:layout_height="56dp"
android:layout_below="@id/edit2right"
android:hint="删除后的数据" />
<!--接下来就是控制我们的按钮-->
<Button
android:id="@+id/insert"
android:text="插入数据"
android:layout_toRightOf="@id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_del"
android:text="删除"
android:layout_toRightOf="@id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/update"
android:text="更新数据"
android:layout_toRightOf="@id/edit2right"
android:layout_below="@id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_del2"
android:text="删除"
android:layout_toRightOf="@id/update"
android:layout_below="@id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_delete"
android:text="执行删除"
android:layout_below="@id/edit2right"
android:layout_toRightOf="@id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_del3"
android:text="删除"
android:layout_toRightOf="@id/update"
android:layout_below="@id/edit2right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--最后的两个按钮查询所有和清除所有-->
<Button
android:id="@+id/find_all"
android:text="查询所有"
android:layout_below="@id/del"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/delete_all"
android:text="清除所有"
android:layout_below="@id/find_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text_view"
android:layout_below="@id/delete_all"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</RelativeLayout>
- 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
- 93
- 94
- 95
- 96
- 97
- 98
- 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
- 93
- 94
- 95
- 96
- 97
- 98
然后就是我们的一个就是的MyHelper类
的代码如下:
package com.example.android_testanddenglu;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(@Nullable Context context) {
super(context, "database", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(id varchar(20),name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}
- 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
- 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
然后是我们的就是Mainactivity的作用:
package com.example.android_testanddenglu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyHelper myHelper;
private EditText edit1;
private EditText edit2left;
private EditText del;
private Button insert;
private Button update;
private Button find_all;
private Button btn_delete;
private String id;
private String name;
private SQLiteDatabase db;
private TextView text_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init() {
edit1 = (EditText) findViewById(R.id.edit1);
edit2left = (EditText) findViewById(R.id.edit2left);
del = (EditText) findViewById(R.id.del);
text_view = (TextView) findViewById(R.id.text_view);
insert = (Button) findViewById(R.id.insert);
update = (Button) findViewById(R.id.update);
find_all = (Button) findViewById(R.id.find_all);
btn_delete = (Button) findViewById(R.id.btn_delete);
insert.setOnClickListener(this);
update.setOnClickListener(this);
find_all.setOnClickListener(this);
btn_delete.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.insert:
id = edit1.getText().toString();
name = edit2left.getText().toString();
db = myHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("id",id);
values.put("name", name);
db.insert("information",null,values);
db.close();
break;
case R.id.update:
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name", name = edit2left.getText().toString() );
db.update("information", values, "id= ? ", new String[]{ edit1.getText().toString()} );
db.close();
break;
case R.id.find_all:
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information", null,null,null,null,null,null);
while (cursor.moveToNext()){
text_view.setText("\n"+"id"+cursor.getString(0)+"name"+cursor.getString(1));
}
cursor.close();
db.close();
break;
case R.id.btn_delete:
db = myHelper.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"数据已经删除",Toast.LENGTH_SHORT).show();
text_view.setText("");
db.close();
break;
}
}
}
- 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
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 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
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112