如何获得像editText这样的用户输入然后插入数据库sqlite?
Posted
技术标签:
【中文标题】如何获得像editText这样的用户输入然后插入数据库sqlite?【英文标题】:How to get user input like editText then insert to database sqlite? 【发布时间】:2020-12-24 12:17:56 【问题描述】:当单击保存按钮插入到 sqlite 数据库时,我想获得像 editText 这样的用户输入。如何做到这一点。我正在寻找,但我找不到它。我正在编写一个记事本应用程序。参考image
package com.suleymanemre.notepad;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class note extends AppCompatActivity
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
button= findViewById(R.id.save);
editText = findViewById(R.id.editText);
public void save(View view)
SQLiteDatabase noteDatabase = openOrCreateDatabase("Notes",MODE_PRIVATE,null);
noteDatabase.execSQL("CREATE TABLE IF NOT EXISTS Notes(Notes VARCHAR);");
public void esc (View view)
super.onBackPressed();
【问题讨论】:
【参考方案1】:您需要准备一个扩展 SQLiteOpenHelper 的类。
此链接可能有助于创建备忘录应用程序。
https://www.javahelps.com/2015/07/android-memo-application.html?m=1
【讨论】:
【参考方案2】:如果您只想保存 1 个值,请考虑使用 SharedPreference:
SharedPreference mPref = context.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE);
//get value
return mPref.getString(Key.YOUR_KEY, DEFAULT_VALUE_IN_STRING);
//add value
SharedPreferences.Editor mEditor = mPref.edit();
mEditor.putString(Key.YOUR_KEY, YOUR_VALUE);
mEditor.commit()
如果你想使用 SQLite 数据库,考虑使用 Room:
将此添加到您的 build.gradle 文件中:
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
创建您的数据库类:
@androidx.room.Dao
public interface MyDao
@Query("SELECT * FROM MyClass")
List<MyClass> getAll();
@Insert
void insertAll(List<MyClass> mList);
@Insert
void insert(MyClass mItem);
创建您的应用数据库类:
@Database(entities = MyClass.class, version = 1)
public abstract class MyAppDatabase extends RoomDatabase
public abstract MyDao MyDao();
创建你的单例类:
public class MyDatabaseClient
private Context mCtx;
private static MyDatabaseClient mInstance;
private MyAppDatabase myAppDatabase;
private MyDatabaseClient(Context mCtx)
this.mCtx = mCtx;
myAppDatabase = Room.databaseBuilder(mCtx, MyClass.class, "MyDatabaseName").build();
public static synchronized MyDatabaseClient getInstance(Context mCtx)
if (mInstance == null)
mInstance = new MyDatabaseClient(mCtx);
return mInstance;
public MyAppDatabase getMyAppDatabase()
return myAppDatabase;
您的项目类别(随时添加新项目以保存或检索:
@Entity
public class MyClass implements Serializable
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "uid")
public int uid;
@ColumnInfo(name = "name")
public String name;
public int getUid()
return uid;
public void setUid(int uid)
this.uid = uid;
public String getName()
return name;
public void setName(String name)
this.name = name;
用法:
//Get All
MyDatabaseClient.getInstance(getApplicationContext()).getMyAppDatabase().MyDao().getAll();
//Insert
final List<MyClass> mList = new ArrayList<>();
MyClass items = new MyClass();
items.setName("YOUR_VALUE");
mList.add(items);
MyDatabaseClient.getInstance(getApplicationContext()).getMyAppDatabase().MyDao().insertAll(mList);
【讨论】:
我是新手编程。我不明白。你为什么写以上是关于如何获得像editText这样的用户输入然后插入数据库sqlite?的主要内容,如果未能解决你的问题,请参考以下文章