因此,我正在将手放入Android开发中,并开始制作电话簿应用程序 我的应用程序有一些活动 - 主要活动,添加联系活动和查看所有活动。
目前每当我尝试添加联系人并按保存时,Android应用程序停止工作,我认为是因为空值最终在数据库中或者输入错误的信息到数据库
只是想知道是否有人可以稍微看一下,看看他们是否能发现任何遗失的东西。
我认为问题出在第33行的此活动,因为TOAST消息不会出现,只要按下保存数据按钮就会崩溃
package uk.ac.northumbria.securephonebook;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Michael on 10/03/2017.
*/
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "DBHELPER";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "contact";
public static final String FIELD_ID = "_id";
public static final String FIELD_FIRSTNAME = "firstname";
public static final String FIELD_SURNAME = "surname";
public static final String FIELD_TELEPHONE = "telephone";
public static final String FIELD_EMAIL = "email";
public static final String QUERY_CREATE = "CREATE TABLE "+TABLE_NAME+" ( "+FIELD_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+FIELD_FIRSTNAME+" VARCHAR (100),"+FIELD_SURNAME+" VARCHAR (100), "+FIELD_TELEPHONE+" VARCHAR (100), "+FIELD_EMAIL+" VARCHAR (100)";
public DBHelper(Context context) {
super(context, DATABASE_NAME , null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL (QUERY_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
修复按钮问题后,现在会生成以下内容 android.database.sqlite.SQLiteException:没有这样的表:contacts(代码1):,同时编译:INSERT INTO联系人(电子邮件,姓氏,名字,电话)VALUES(?,?,?,?)
答案 0 :(得分:0)
QUERY_CREATE缺少')'最后。
更改此
public static final String QUERY_CREATE = "CREATE TABLE "+TABLE_NAME+" ( "+FIELD_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+FIELD_FIRSTNAME+" VARCHAR (100),"+FIELD_SURNAME+" VARCHAR (100), "+FIELD_TELEPHONE+" VARCHAR (100), "+FIELD_EMAIL+" VARCHAR (100)";
到
public static final String QUERY_CREATE = "CREATE TABLE "+TABLE_NAME+" ( "+FIELD_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+FIELD_FIRSTNAME+" VARCHAR (100),"+FIELD_SURNAME+" VARCHAR (100), "+FIELD_TELEPHONE+" VARCHAR (100), "+FIELD_EMAIL+" VARCHAR (100) )";