我现在已经摸不着头脑了几个小时,试图找出我的安卓程序崩溃的原因。
我遇到了两个不同的错误:
当程序启动后我按下按钮时会发生这种情况。
我有5个不同的类,两个处理数据库,另外3个处理程序的信息和功能。
logcat的
Process: com.example.rockodile.contactsappliaction, PID: 11603
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rockodile.contactsappliaction/com.example.rockodile.contactsappliaction.ContactDetails}: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT id, forename, surname, housenumber, street, town, county, postcode, phone, FROM contacts WHERE id=?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT id, forename, surname, housenumber, street, town, county, postcode, phone, FROM contacts WHERE id=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.example.rockodile.contactsappliaction.UserSQL.getContactById(UserSQL.java:136)
at com.example.rockodile.contactsappliaction.ContactDetails.onCreate(ContactDetails.java:58)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Logcat显示有两个问题,一个是我在第136行的UserSQL类
另一个是在第58行的ContactDetails类
上public class UserSQL {
private DBHelper dbHelper;
public UserSQL(Context context) {
dbHelper = new DBHelper(context);
}
public int insert(Contact contact) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Contact.KEY_FORENAME, contact.forename);
values.put(Contact.KEY_SURNAME, contact.surname);
values.put(Contact.KEY_HOUSENUMEBR, contact.housenumber);
values.put(Contact.KEY_STREET, contact.street);
values.put(Contact.KEY_TOWN, contact.town);
values.put(Contact.KEY_COUNTY, contact.county);
values.put(Contact.KEY_POSTCODE, contact.postcode);
values.put(Contact.KEY_PHONE, contact.phone);
values.put(Contact.KEY_EMAIL, contact.email);
// Inserting Row
long contact_Id = db.insert(Contact.TABLE, null, values);
db.close(); // Closing database connection
return (int) contact_Id;
}
public void delete(int contact_Id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// It's a good practice to use parameter ?, instead of concatenate string
db.delete(Contact.TABLE, Contact.KEY_ID + "= ?", new String[] { String.valueOf(contact_Id) });
db.close(); // Closing database connection
}
public void update(Contact contact) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Contact.KEY_FORENAME, contact.forename);
values.put(Contact.KEY_SURNAME, contact.surname);
values.put(Contact.KEY_HOUSENUMEBR, contact.housenumber);
values.put(Contact.KEY_STREET, contact.street);
values.put(Contact.KEY_TOWN, contact.town);
values.put(Contact.KEY_COUNTY, contact.county);
values.put(Contact.KEY_POSTCODE, contact.postcode);
values.put(Contact.KEY_PHONE, contact.phone);
values.put(Contact.KEY_EMAIL, contact.email);
// It's a good practice to use parameter ?, instead of concatenate string
db.update(Contact.TABLE, values, Contact.KEY_ID + "= ?", new String[] {String.valueOf(contact.contact_ID) });
db.close(); // Closing database connection
}
public ArrayList<HashMap<String, String>> getContactList() {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Contact.KEY_ID + "," +
Contact.KEY_FORENAME + ", " +
Contact.KEY_SURNAME + ", " +
Contact.KEY_HOUSENUMEBR + ", " +
Contact.KEY_STREET + ", " +
Contact.KEY_TOWN + ", " +
Contact.KEY_COUNTY + ", " +
Contact.KEY_POSTCODE + ", " +
Contact.KEY_PHONE + ", " +
" FROM " + Contact.TABLE;
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
HashMap<String, String> record = new HashMap<String, String>();
record.put("id", cursor.getString(cursor.getColumnIndex(Contact.KEY_ID)));
record.put("forename", cursor.getString(cursor.getColumnIndex(Contact.KEY_FORENAME)));
record.put("surname", cursor.getString(cursor.getColumnIndex(Contact.KEY_SURNAME)));
record.put("housenumber", cursor.getString(cursor.getColumnIndex(Contact.KEY_HOUSENUMEBR)));
record.put("street", cursor.getString(cursor.getColumnIndex(Contact.KEY_STREET)));
record.put("town", cursor.getString(cursor.getColumnIndex(Contact.KEY_TOWN)));
record.put("county", cursor.getString(cursor.getColumnIndex(Contact.KEY_COUNTY)));
record.put("postcode", cursor.getString(cursor.getColumnIndex(Contact.KEY_POSTCODE)));
record.put("phone", cursor.getString(cursor.getColumnIndex(Contact.KEY_PHONE)));
record.put("email", cursor.getString(cursor.getColumnIndex(Contact.KEY_EMAIL)));
contactList.add(record);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
public Contact getContactById(int Id){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Contact.KEY_ID + ", " +
Contact.KEY_FORENAME + ", " +
Contact.KEY_SURNAME + ", " +
Contact.KEY_HOUSENUMEBR + ", " +
Contact.KEY_STREET + ", " +
Contact.KEY_TOWN + ", " +
Contact.KEY_COUNTY + ", " +
Contact.KEY_POSTCODE + ", " +
Contact.KEY_PHONE + ", " +
" FROM " + Contact.TABLE
+ " WHERE " +
Contact.KEY_ID + "=?";// It's a good practice to use parameter ?, instead of concatenate string
int iCount =0;
Contact contact = new Contact();
Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(Id) } );
if (cursor.moveToFirst()) {
do {
contact.contact_ID =cursor.getInt(cursor.getColumnIndex(Contact.KEY_ID));
contact.forename =cursor.getString(cursor.getColumnIndex(Contact.KEY_FORENAME));
contact.surname =cursor.getString(cursor.getColumnIndex(Contact.KEY_SURNAME));
contact.housenumber =cursor.getInt(cursor.getColumnIndex(Contact.KEY_HOUSENUMEBR));
contact.street =cursor.getString(cursor.getColumnIndex(Contact.KEY_STREET));
contact.town =cursor.getString(cursor.getColumnIndex(Contact.KEY_TOWN));
contact.county =cursor.getString(cursor.getColumnIndex(Contact.KEY_COUNTY));
contact.postcode =cursor.getString(cursor.getColumnIndex(Contact.KEY_POSTCODE));
contact.phone =cursor.getInt(cursor.getColumnIndex(Contact.KEY_PHONE));
contact.email =cursor.getString(cursor.getColumnIndex(Contact.KEY_EMAIL));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contact;
}
}
public class ContactDetails extends ActionBarActivity implements android.view.View.OnClickListener {
Button btnSave , btnDelete;
Button btnClose;
EditText forename;
EditText surname;
EditText housenumber;
EditText street;
EditText town;
EditText county;
EditText postcode;
EditText phone;
EditText email;
private int _contact_Id = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_detail);
btnSave = (Button) findViewById(R.id.save);
btnDelete = (Button) findViewById(R.id.delete);
btnClose = (Button) findViewById(R.id.close);
forename = (EditText) findViewById(R.id.forename);
surname = (EditText) findViewById(R.id.surname);
housenumber = (EditText) findViewById(R.id.housenumber);
street = (EditText) findViewById(R.id.street);
town = (EditText) findViewById(R.id.town);
county = (EditText) findViewById(R.id.county);
postcode = (EditText) findViewById(R.id.postcode);
phone = (EditText) findViewById(R.id.phone);
email = (EditText) findViewById(R.id.email);
btnSave.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnClose.setOnClickListener(this);
_contact_Id =0;
Intent intent = getIntent();
_contact_Id =intent.getIntExtra("contact_Id", 0);
UserSQL repo = new UserSQL(this);
Contact contact_information;
contact_information = repo.getContactById(_contact_Id);
housenumber.setText(String.valueOf(contact_information.housenumber));
surname.setText(contact_information.surname);
forename.setText(contact_information.forename);
street.setText(contact_information.street);
town.setText(contact_information.town);
county.setText(contact_information.county);
postcode.setText(contact_information.postcode);
phone.setText(String.valueOf(contact_information.phone));
email.setText(contact_information.email);
}
public void onClick(View view) {
if (view == findViewById(R.id.save)) {
UserSQL repo = new UserSQL(this);
Contact contact = new Contact();
contact.housenumber = Integer.parseInt(housenumber.getText().toString());
contact.phone = Integer.parseInt(phone.getText().toString());
contact.forename = forename.getText().toString();
contact.surname = surname.getText().toString();
contact.street = street.getText().toString();
contact.town = town.getText().toString();
contact.county = county.getText().toString();
contact.postcode = postcode.getText().toString();
contact.contact_ID = _contact_Id;
if (_contact_Id == 0) {
_contact_Id = repo.insert(contact);
Toast.makeText(this, "New contact created", Toast.LENGTH_SHORT).show();
} else {
repo.update(contact);
Toast.makeText(this, "Contact updated", Toast.LENGTH_SHORT).show();
}
} else if (view == findViewById(R.id.delete)) {
UserSQL repo = new UserSQL(this);
repo.delete(_contact_Id);
Toast.makeText(this, "Contact Record Deleted", Toast.LENGTH_SHORT);
finish();
} else if (view == findViewById(R.id.close)) {
finish();
}
}
}
程序运行后,我有两个按钮,一个用于查看联系人,另一个用于添加新联系人。按任何这些按钮会导致程序关闭并崩溃。
然而,当我按下每个时,我会得到不同的错误。 您在上面看到的错误是我按下ADD按钮。 按下VIEW按钮时出现的错误如下
android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT id,forename, surname, housenumber, street, town, county, postcode, phone, FROM contacts
at com.example.rockodile.contactsappliaction.UserSQL.getContactList(UserSQL.java:90)
at com.example.rockodile.contactsappliaction.MainActivity.onClick(MainActivity.java:52)
第二个箭头的完整logcat位于下方。
FATAL EXCEPTION: main
Process: com.example.rockodile.contactsappliaction, PID: 13715
android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT id,forename, surname, housenumber, street, town, county, postcode, phone, FROM contacts
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.example.rockodile.contactsappliaction.UserSQL.getContactList(UserSQL.java:90)
at com.example.rockodile.contactsappliaction.MainActivity.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我只是想知道问题所在,我已经尝试了所有建议的内容。我错过了一些明显的东西吗我之前使用过这种方法并检查过以前的版本,它的工作正常并且完全相同但是这个版本不起作用。
我非常感谢任何建议
答案 0 :(得分:0)
抱歉,我无法阅读您的所有代码。但我只是快速找到了你的Sql语法:
l = [1,2,3,4]
l2 = [tuple(l)]
SELECT id,forename, surname, housenumber, street, town, county, postcode, phone, FROM contacts
,
纠正一个:
FROM
答案 1 :(得分:0)
删除&#34;,&#34;在选择查询中的KEY_PHONE之后
String selectQuery = "SELECT " +
Contact.KEY_ID + ", " +
Contact.KEY_FORENAME + ", " +
Contact.KEY_SURNAME + ", " +
Contact.KEY_HOUSENUMEBR + ", " +
Contact.KEY_STREET + ", " +
Contact.KEY_TOWN + ", " +
Contact.KEY_COUNTY + ", " +
Contact.KEY_POSTCODE + ", " +
Contact.KEY_PHONE +
" FROM " + Contact.TABLE
+ " WHERE " +
Contact.KEY_ID + "=?";// It's a good practice to use parameter ?, instead of concatenate string
答案 2 :(得分:0)
如果这会产生问题,请尝试使用
Cursor rows= db.query(TableFile.Table_Name, columns, null, null,null,null,null);
检查参数。 1.表名 2.你想要的列数组 3.&#34; columnName1 =?&#34; 4. columnName1的值的数组参数
rest keep null
希望这有帮助。
答案 3 :(得分:0)
请在此处检查此错误。
Process: com.example.rockodile.contactsappliaction, PID: 5549
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.rockodile.contactsappliaction.UserSQL.getContactList(UserSQL.java:105)
at com.example.rockodile.contactsappliaction.MainActivity.onClick(MainActivity.java:52)
更新
public ArrayList<HashMap<String, String>> getContactList() {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Contact.KEY_ID + "," +
Contact.KEY_FORENAME + ", " +
Contact.KEY_SURNAME + ", " +
Contact.KEY_HOUSENUMEBR + ", " +
Contact.KEY_STREET + ", " +
Contact.KEY_TOWN + ", " +
Contact.KEY_COUNTY + ", " +
Contact.KEY_POSTCODE + ", " +
Contact.KEY_PHONE + ", " +
" FROM " + Contact.TABLE;
来自
之前的逗号