android studio 获取edittext_Android数据库SQLite增改查删操作演示

本文通过一个实际的Android Studio项目展示了如何进行SQLite数据库的增、删、改、查操作。在UI设计中,使用了edittext输入数据,并通过按钮触发不同操作。代码中创建了DatabaseHelper类,实现了SQLiteOpenHelper的抽象方法,用于数据库和表的创建。MainActivity中初始化了界面元素并设置了点击事件,实现数据的交互。
摘要由CSDN通过智能技术生成

SQLite增改查删操作演示

SQLite增改查删操作演示-leansmall

《1》 效果:

a5436278ce727af7b458655881420ea9.png

《2》 UI设计:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:layout_marginBottom="50dp"

>

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="SQLite增改查删操作演示"

android:textColor="#03A9F4"

android:textSize="30sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:orientation="horizontal">

android:id="@+id/inset_edittext"

android:layout_width="0dp"

android:layout_weight="5"

android:layout_height="wrap_content"

android:hint="请输入要插入的数据"

android:textColorHint="#CCCCCC"/>

android:id="@+id/insert"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="增加" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/update_before_edittext"

android:layout_width="0dp"

android:layout_weight="2.5"

android:layout_height="wrap_content"

android:hint="请输入更新前的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/update_after_edittext"

android:layout_width="0dp"

android:layout_weight="2.5"

android:layout_height="wrap_content"

android:hint="请输入更新后的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/update"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="修改" />

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/delete_edittext"

android:layout_width="0dp"

android:layout_weight="5"

android:layout_height="wrap_content"

android:hint="请输入要删除的内容"

android:textColorHint="#CCCCCC"/>

android:id="@+id/delete"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="删除" />

android:id="@+id/query"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="查询所有"/>

android:id="@+id/clear"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="清除所有" />

android:id="@+id/textview"

android:layout_width="match_parent"

android:layout_height="300dp"

android:layout_marginVertical="50dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:hint="查询内容为空"

android:textColorHint="#CCCCCC"

android:textColor="#6666FF"

android:textSize="25dp"/>

《3》 代码设计:

//1. DatabaseHelper.java 设计

public class DatabaseHelper extends SQLiteOpenHelper{

public static final String TB_NAME = "user";

//带全部参数的构造函数,此构造函数必不可少

public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {

super(context, name, factory, version);

}

@Override

public void onCreate(SQLiteDatabase db) {

//创建数据库的一个表格,表格名为user,执行sql语句

String sql = "CREATE TABLE IF NOT EXISTS " + TB_NAME +"name varchar(20)";

db.execSQL(sql);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TB_NAME);

onCreate(db);

}

}

//2. MainActivity.java 设计

public class MainActivity extends AppCompatActivity{

//声明所有要用到的按钮,文本框,编辑框的对象

Button insert;

Button update;

Button delete;

Button query;

Button clear;

EditText insert_edittext;

EditText delete_edittext;

EditText update_before_edittext;

EditText update_after_edittext;

TextView textview;

DatabaseHelper dbHelper;

SQLiteDatabase db;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//根据Layout按钮id得到Java按钮对象

insert = (Button) findViewById(R.id.insert);//增加按钮

update = (Button) findViewById(R.id.update);//修改按钮

delete = (Button) findViewById(R.id.delete);//删除按钮

query = (Button) findViewById(R.id.query);//查询按钮

clear = (Button)findViewById(R.id.clear);//清除编辑框和文本框内容的按钮

//因为响应点击按钮事件时要操作文本输入框中的内容

// 所以要获取相应文本输入框的对象及其中输入内容

insert_edittext = (EditText)findViewById(R.id.inset_edittext);

delete_edittext = (EditText)findViewById(R.id.delete_edittext);

update_before_edittext = (EditText)findViewById(R.id.update_before_edittext);

update_after_edittext = (EditText)findViewById(R.id.update_after_edittext);

textview = (TextView)findViewById(R.id.textview);//放置查询内容的文本框

//依靠DatabaseHelper的构造函数创建数据库

dbHelper = new DatabaseHelper(MainActivity.this, "test_db

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值