java - 我是 android 领域的新手,并且遇到插入 android 应用程序的问题?
问题描述
我做了一个简单的插入形式,它将三个数字作为输入存储在数据库中(请注意我正在使用 sqlite 数据库)。所以主要的是当我点击提交时,它会出现一个错误,上面写着没有这样的表:-messgae_table。我试图找出我的代码有什么问题,但对我来说什么都没有解决!!!!请帮帮我......
Activity.java 主页面:-
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
DatabaseQueries db=new DatabaseQueries(Main2Activity.this);
Log.e("data",editText1.getText().toString());
long result=db.addMessageNumber(editText1.getText().toString(),editText2.getText().toString(),editText3.getText().toString());
if (result>0){
Toast.makeText(Main2Activity.this, "Inserted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Main2Activity.this, "Not Inserted", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.getMessage();
e.printStackTrace();
}
DatabaseColumn.java:-
public static final String MESSAGE_TABLE = "message_table";
public static final String MESSAGE_NUMBERONE = "message_numberone";
public static final String MESSAGE_NUMBERTWO = "messgae_numbertwo";
public static final String MESSAGE_NUMBERTHREE = "message_numberthree";
数据库创建:-
public class DatabaseCreation extends SQLiteOpenHelper {
private static String DB_NAME = "secure.db";
private static int DB_VERSION = 1;
private String message_table = "CREATE TABLE " + DatabaseColumn.MESSAGE_TABLE + " ( " + DatabaseColumn.MESSAGE_NUMBERONE + " TEXT, " + DatabaseColumn.MESSAGE_NUMBERTWO + " TEXT, " + DatabaseColumn.MESSAGE_NUMBERTHREE + " TEXT )";
public DatabaseCreation(Context context){
super(context, DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(contact_table);
sqLiteDatabase.execSQL(message_table);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
数据库查询:-
public long addMessageNumber(String numberone, String numbertwo, String numberthree){
SQLiteDatabase sqLiteDatabase2=db.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(DatabaseColumn.MESSAGE_NUMBERONE,numberone);
cv.put(DatabaseColumn.MESSAGE_NUMBERTWO,numbertwo);
cv.put(DatabaseColumn.MESSAGE_NUMBERTHREE,numberthree);
long result=sqLiteDatabase2.insert(DatabaseColumn.MESSAGE_TABLE,null,cv);
sqLiteDatabase2.close();
return result;
}
解决方案
您的代码,除了那一行之外,sqLiteDatabase.execSQL(contact_table);
在拼凑在一起时似乎可以工作。
- 上面的行被注释掉了。
因此,您的问题可能是您习惯于onCreate
每次运行应用程序时都运行。即当一个活动开始时。但是,SQLiteOpenHelper
'sonCreate
方法仅在创建数据库时运行一次。任何其他时间(即数据库存在时)它都不会运行。
因此,您的问题可能是找不到该表,因为它尚未创建,因为您已将代码添加到onCreate
(以创建表)而不删除数据库。
使固定 :-
FIX是执行以下操作之一:-
- 通过设置删除应用程序的数据,或
- 卸载应用程序(删除应用程序的数据)
完成上述其中一项操作后,重新运行应用程序,这可能会让您通过 Table not found 错误。
用于测试的代码:-
数据库列.java
public class DatabaseColumn {
public static final String MESSAGE_TABLE = "message_table";
public static final String MESSAGE_NUMBERONE = "message_numberone";
public static final String MESSAGE_NUMBERTWO = "messgae_numbertwo";
public static final String MESSAGE_NUMBERTHREE = "message_numberthree";
}
数据库创建.java
public class DatabaseCreation extends SQLiteOpenHelper {
private static String DB_NAME = "secure.db";
private static int DB_VERSION = 1;
private String message_table =
"CREATE TABLE " + DatabaseColumn.MESSAGE_TABLE + " ( " +
DatabaseColumn.MESSAGE_NUMBERONE + " TEXT, " +
DatabaseColumn.MESSAGE_NUMBERTWO + " TEXT, " +
DatabaseColumn.MESSAGE_NUMBERTHREE + " TEXT )";
public DatabaseCreation(Context context){
super(context, DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//sqLiteDatabase.execSQL(contact_table); //<<<<<<<< ?????????
sqLiteDatabase.execSQL(message_table);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
-注意注释掉的行(您可能不必将其注释掉)
数据库查询.java
public class DatabaseQueries {
DatabaseCreation db;
public DatabaseQueries(Context context) {
db = new DatabaseCreation(context);
}
//!!!!!!!!!! Code other than this block has been assumed
public long addMessageNumber(String numberone, String numbertwo, String numberthree){
SQLiteDatabase sqLiteDatabase2=db.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(DatabaseColumn.MESSAGE_NUMBERONE,numberone);
cv.put(DatabaseColumn.MESSAGE_NUMBERTWO,numbertwo);
cv.put(DatabaseColumn.MESSAGE_NUMBERTHREE,numberthree);
long result=sqLiteDatabase2.insert(DatabaseColumn.MESSAGE_TABLE,null,cv);
sqLiteDatabase2.close();
return result;
}
}
- 注意见评论
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText1,editText2,editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = this.findViewById(R.id.button);
editText1 = this.findViewById(R.id.edittext1);
editText2 = this.findViewById(R.id.edittext2);
editText3 = this.findViewById(R.id.edittext3);
//!!!!!!!!!! Code other than this block has been assumed
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
//!!!!!!!!!! view.getContext() used instead of Main2Activity.this (code is easier to copy elsewhere)
DatabaseQueries db = new DatabaseQueries(view.getContext());
Log.e("data", editText1.getText().toString());
long result = db.addMessageNumber(
editText1.getText().toString(),
editText2.getText().toString(),
editText3.getText().toString()
);
if (result > 0) {
Toast.makeText(view.getContext(), "Inserted", Toast.LENGTH_SHORT).show();
Log.e("INSERTINFO","Row inserted."); //<<<<<<<<< ADDED FOR TESTING
} else {
Toast.makeText(view.getContext(), "Not Inserted", Toast.LENGTH_SHORT).show();
Log.e("INSERTINFO","Row NOT inserted."); //<<<<<<<<< ADDED FOR TESTING
}
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
});
}
}
- 注意MainActivity 用于测试而不是 Main2Activity
- 见评论。
- 吐司的补充是伐木。
测试结果
启动时首次运行:-
插入
在三个 EditTexts 中输入 T1、T2 和 T3 并单击 CLICKME 按钮后,日志包含:-
2019-11-29 07:13:49.056 E/data: T1 2019-11-29 07:13:49.093 E/INSERTINFO: Row inserted.
推荐阅读
- javascript - 附加html代码后如何获取就绪事件
- html - spring boot 中的 href 问题
- reactjs - Connected-React-Route 更改仍然导致 typeError 出现
- extjs6.5.1 - 如何获取应用程序的版本
- html - 不同分辨率的视频和视频预览图(海报)
- flutter - 升级后出现问题
- node.js - 有没有办法使用聚合从猫鼬中获取 ObjectId 的日期?
- python - 如何选择特定事件前的最后 x 天?
- c++ - 如何调用运行时链接函数?
- python-3.x - 当我在 pyqt 和 python 中使用 while 循环作为启动函数时,我的 gui 没有出现