热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidStudioSQLite数据库增删改查简单(代码参考)

一个建表一个执行crud操作建表代码importandroid.content.Context;importandroid.database.sqlite.SQLiteDat

一个建表
一个执行crud操作

建表代码

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)");}//创建表 表名information 表结构 自增id,字符串姓名,int年龄public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.d("myDeBug","数据库版本已更新");}//数据库版本发生变化时调用
}

执行crud代码

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;import java.util.ArrayList;
import java.util.List;public class DictActivity extends AppCompatActivity {private Button insertButton,updateButton,searchButton,deleteButton;private EditText name,age;private TextView show,showAge;final DatabaseHelper dbHelper=new DatabaseHelper(DictActivity.this);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dict);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();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"));int newAge = cursor.getInt(cursor.getColumnIndex("age"));show.setText(show.getText() + "\n" + newName);showAge.setText(showAge.getText()+"\n" + newAge);}cursor.close();}

界面布局

<?xml version&#61;"1.0" encoding&#61;"utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"><LinearLayoutandroid:orientation&#61;"horizontal"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"姓名"android:textSize&#61;"30sp"/><EditTextandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:hint&#61;"请输入姓名"android:textSize&#61;"20sp"android:id&#61;"&#64;&#43;id/name"/></LinearLayout><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"年龄"android:textSize&#61;"30sp"/><EditTextandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:hint&#61;"请输入年龄"android:textSize&#61;"20sp"android:id&#61;"&#64;&#43;id/age"/></LinearLayout><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:orientation&#61;"horizontal"><Buttonandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:textSize&#61;"20sp"android:text&#61;"插入"android:id&#61;"&#64;&#43;id/btn_insert"android:textAllCaps&#61;"false"/><Buttonandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:textSize&#61;"20sp"android:text&#61;"更新"android:id&#61;"&#64;&#43;id/btn_update"android:textAllCaps&#61;"false"/><Buttonandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:textSize&#61;"20sp"android:text&#61;"查询"android:id&#61;"&#64;&#43;id/btn_search"android:textAllCaps&#61;"false"/><Buttonandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:textSize&#61;"20sp"android:text&#61;"删除"android:id&#61;"&#64;&#43;id/btn_delete"android:textAllCaps&#61;"false"/></LinearLayout><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:orientation&#61;"horizontal"><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:id&#61;"&#64;&#43;id/tv_show"android:textSize&#61;"20sp"android:gravity&#61;"center_horizontal"/><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_weight&#61;"1"android:id&#61;"&#64;&#43;id/tv_showAge"android:textSize&#61;"20sp"android:gravity&#61;"center_horizontal"/>
</LinearLayout></LinearLayout>


推荐阅读
author-avatar
是不是有谁代替我陪在你身旁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有