我是Java和Android开发的新手,每当我运行我的活动并尝试在数据库中添加新记录时,我都会收到SQLiteException错误。
我在这里问了很多文档,然后在这个页面上搜索了答案,但是找不到问题的解决方案,如果有人能帮助我找到我所知道的话,我真的很感激。在这里失踪。
我的logcat:
07-21 19:29:13.060 1187-1187/com.lysandros.mars E/SQLiteLog﹕ (1) near"Standards": syntax error
07-21 19:29:13.099 1187-1187/com.lysandros.mars E/SQLiteDatabase﹕ Error inserting Induction Standards Met=false Business Number= Date of Birth= Workplace= Notes= Name= Email= Telephone Number= Sick Pay=Sickness Pay Job Title=Deputy Manager Address= Surname= Department= Start Date=
android.database.sqlite.SQLiteException: near "Standards": syntax error (code 1): , while compiling: INSERT INTO EmployeeTable(Induction Standards Met,Business Number,Date of Birth,Workplace,Notes,Name,Email,Telephone Number,Sick Pay,Job Title,Address,Surname,Department,Start Date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.lysandros.mars.EmployeeDatabaseAdapter.insertData(EmployeeDatabaseAdapter.java:53)
at com.lysandros.mars.Form$1.onClick(Form.java:78)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
包含其中一个错误的数据库活动部分:
public class EmployeeDatabaseAdapter {
DatabaseHelper helper;
public EmployeeDatabaseAdapter(Context context) {
helper = new DatabaseHelper(context);
}
public long insertData (String name,
String surname,
String tnumber,
String bnumber,
String email,
String address,
String dob,
String department,
String jobtitle,
String workplace,
String stardate,
Boolean ISM,
Boolean ISM2,
String spay,
String notes)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COLUMN_NAME, name);
contentValues.put(DatabaseHelper.COLUMN_SURNAME, surname);
contentValues.put(DatabaseHelper.COLUMN_NUMBER, tnumber);
contentValues.put(DatabaseHelper.COLUMN_BNUMBER, bnumber);
contentValues.put(DatabaseHelper.COLUMN_EMAIL, email);
contentValues.put(DatabaseHelper.COLUMN_ADDRESS, address);
contentValues.put(DatabaseHelper.COLUMN_DOB, dob);
contentValues.put(DatabaseHelper.COLUMN_DEPARTMENT, department);
contentValues.put(DatabaseHelper.COLUMN_JOBTITLE, jobtitle);
contentValues.put(DatabaseHelper.COLUMN_WORKPLACE, workplace);
contentValues.put(DatabaseHelper.COLUMN_STARTDATE, stardate);
contentValues.put(DatabaseHelper.COLUMN_STANDARDS, ISM);
contentValues.put(DatabaseHelper.COLUMN_STANDARDS2, ISM2);
contentValues.put(DatabaseHelper.COLUMN_SPAY, spay);
contentValues.put(DatabaseHelper.COLUMN_NOTES, notes);
long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
return id;
}
static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "EmployeeDatabase";
private static final String TABLE_NAME = "EmployeeTable";
private static final int DATABASE_VERSION = 1;
private static final String COLUMN_UID = "_id";
private static final String COLUMN_NAME = "Name";
private static final String COLUMN_SURNAME = "Surname";
private static final String COLUMN_NUMBER = "Telephone Number";
private static final String COLUMN_BNUMBER = "Business Number";
private static final String COLUMN_EMAIL = "Email";
private static final String COLUMN_ADDRESS = "Address";
private static final String COLUMN_DOB = "Date of Birth";
private static final String COLUMN_DEPARTMENT = "Department";
private static final String COLUMN_JOBTITLE = "Job Title";
private static final String COLUMN_WORKPLACE = "Workplace";
private static final String COLUMN_STARTDATE = "Start Date";
private static final String COLUMN_STANDARDS = "Induction Standards Met";
private static final String COLUMN_STANDARDS2 = "Induction Standards Met";
private static final String COLUMN_SPAY = "Sick Pay";
private static final String COLUMN_NOTES = "Notes";
private static final String COLUMN_HOLIDAYENT = "Holiday Entitlement";
private static final String COLUMN_HOLIDAYTKN = "Holiday Taken";
private static final String COLUMN_HOLIDAYBKD = "Holiday Booked";
private static final String COLUMN_HOLIDAYRMN = "Holiday Remaining";
private static final String COLUMN_TSICKENESS = "Total Sickness";
private static final String COLUMN_TLATENESS = "Total Lateness";
private static final String COLUMN_OTHERABSENCES = "Other Absences";
private static final String COLUMN_SUPERVISIONS = "Supervisions";
private static final String COLUMN_PRBREVIEWS = "Probation Reviews";
private static final String COLUMN_PRFMREVIEWS = "Performance Reviews";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+COLUMN_UID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
" "+COLUMN_NAME+" VARCHAR(255)," +
" "+COLUMN_SURNAME+" VARCHAR(255)," +
" "+COLUMN_NUMBER+" INTEGER," +
" "+COLUMN_BNUMBER+" INTEGER," +
" "+COLUMN_EMAIL+" INTEGER," +
" "+COLUMN_ADDRESS+" VARCHAR(255)," +
" "+COLUMN_DOB+" DATE," +
" "+COLUMN_DEPARTMENT+" VARCHAR(255)," +
" "+COLUMN_JOBTITLE+" VARCHAR(255)," +
" "+COLUMN_WORKPLACE+" VARCHAR(255)," +
" "+COLUMN_STARTDATE+" DATE," +
" "+COLUMN_STANDARDS+" BOOLEAN," +
" "+COLUMN_STANDARDS2+" BOOLEAN, " +
" "+COLUMN_SPAY+" BOOLEAN, " +
" "+COLUMN_NOTES+" TEXT, " +
" "+COLUMN_HOLIDAYENT+" SMALLINT," +
" "+COLUMN_HOLIDAYTKN+" SMALLINT," +
" "+COLUMN_HOLIDAYBKD+" SMALLINT," +
" "+COLUMN_HOLIDAYRMN+" SMALLINT," +
" "+COLUMN_TSICKENESS+" SMALLINT," +
" "+COLUMN_TLATENESS+" SMALLINT," +
" "+COLUMN_OTHERABSENCES+" SMALLINT," +
" "+COLUMN_SUPERVISIONS+" SMALLINT," +
" "+COLUMN_PRBREVIEWS+" SMALLINT," +
" "+COLUMN_PRFMREVIEWS+" SMALLINT);";
我用于在数据库中添加数据的表单活动部分:
final EditText nameField = (EditText) findViewById(R.id.name);
final EditText surnameField = (EditText) findViewById(R.id.surname);
final EditText tnumberField = (EditText) findViewById(R.id.tnumber);
final EditText bnumberFeild = (EditText) findViewById(R.id.bnumber);
final EditText emailField = (EditText) findViewById(R.id.email);
final EditText addressField = (EditText) findViewById(R.id.address);
final EditText dobField = (EditText) findViewById(R.id.dob);
final EditText departmentField = (EditText) findViewById(R.id.department);
final Spinner jobtitleSpinner = (Spinner) findViewById(R.id.jobtitletype);
final EditText workplaceField = (EditText) findViewById(R.id.workplace);
final EditText sdateField = (EditText) findViewById(R.id.sdate);
final CheckBox ismCheckBoxFieldYes = (CheckBox) findViewById(R.id.ISM1);
final CheckBox ismCheckBoxFieldNo = (CheckBox) findViewById(R.id.ISM2);
final Spinner SPAYSpinner = (Spinner) findViewById(R.id.sicknesspaytype);
final EditText notesField = (EditText) findViewById(R.id.notes);
Button addrecord = (Button) findViewById(R.id.AddEmployee);
dbhelper = new EmployeeDatabaseAdapter(this);
addrecord.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = nameField.getText().toString();
String surname = surnameField.getText().toString();
String tnumber = tnumberField.getText().toString();
String bnumber = bnumberFeild.getText().toString();
String email = emailField.getText().toString();
String address = addressField.getText().toString();
String dob = dobField.getText().toString();
String department = departmentField.getText().toString();
String jobtitletype = jobtitleSpinner.getSelectedItem().toString();
String workplace = workplaceField.getText().toString();
String sdate = sdateField.getText().toString();
boolean bRequiresResponse = ismCheckBoxFieldYes.isChecked();
boolean bReuiresResponse2 = ismCheckBoxFieldNo.isChecked();
String SPAY = SPAYSpinner.getSelectedItem().toString();
String notes = notesField.getText().toString();
dbhelper.insertData(name, surname, tnumber, bnumber, email, address, dob, department, jobtitletype, workplace, sdate, bRequiresResponse, bReuiresResponse2, SPAY, notes);
}
});
我是否需要添加其他内容请让我知道..我试图尽可能少地包括所有必要的信息,以确定问题。
答案 0 :(得分:2)
列名称不能包含空格。您需要更改它们,例如Induction_Standards_Met
或InductionStandardsMet
答案 1 :(得分:1)
这与android无关。你的SQL是错误的:
你正在生成这个:
CREATE TABLE (
...
Job Title VARCHAR(255),
...
)
字段/表名称 NOT 中应包含空格。请尝试使用Job_Title
或其他内容,对所有其他内容进行类似操作&#34; spacer&#34;字段。