SQLite查询将不会执行。按钮点击时崩溃

时间:2017-06-06 15:11:47

标签: java android sqlite listview android-sqlite

应用程序成功启动但在按钮上单击它会在ImageButton后面的CustomListView中崩溃

此代码形式为ListView自定义适配器:

holder.submitAttendance.setOnClickListener(new OnClickListener() {
        @Override
       public void onClick(View v) {
            String Lt,teacherName,status = null,startTime,endTime;
            if(holder.radioButtonPresent.isChecked()) 
            {
                status=holder.radioButtonPresent.getText().toString();
            }
            else if(holder.radioButtonAbsent.isChecked())
            {
                status=holder.radioButtonAbsent.getText().toString();
            }
            startTime=holder.etxtStart_time_Picker.getText().toString();
            endTime=holder.etxtEnd_time_Picker.getText().toString();
            Lt=holder.txtlt.getText().toString();
            teacherName=holder.txtteacher.getText().toString();
        dataBaseHelper1.insertData(Lt,teacherName,status,startTime,endTime);
        }
    });

此表单sqlite数据库助手类

public class DataBaseHelper extends SQLiteOpenHelper
{

public static String DATABASE_NAME = "Teacher.db";
public static String TABLE_NAME = "Teacher_Table";
public static String Col_1_LT = "LT";
public static String Col_2_TName = "Teacher_Name";
public static String Col_3_Status = "Status";
public static String Col_4_StartTime = "StartTime";
public static String Col_5_EndTime = "EndTime";

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    String query="create table" + TABLE_NAME +"( LT TEXT PRIMARY 
    KEY,Teacher_Name TEXT, Status TEXT, StartTime TEXT, EndTime TEXT)";
    db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
    ContentValues contentValues = new ContentValues();
    public boolean insertData(String Lt, String TeacherName, String Status,String StartTime, String EndTime) 
   {
    SQLiteDatabase db = this.getWritableDatabase();
    contentValues.put(Col_1_LT, Lt);
    contentValues.put(Col_2_TName, TeacherName);
    contentValues.put(Col_3_Status, Status);
    contentValues.put(Col_4_StartTime, StartTime);
    contentValues.put(Col_5_EndTime, EndTime);
    long result = db.insert(TABLE_NAME, null, contentValues);
    db.close();
    //to check whether Data is Inserted in Database
    if (result == -1) {
        {
            return false;
        }
    } else
        {
        return true;
        }
}}

堆栈跟踪:

beginning of crash
06-06 20:31:56.713 2888-2888/com.practice.testlistviewcustomadapter E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.practice.testlistviewcustomadapter, PID: 2888
    android.database.sqlite.SQLiteException: near "tableTeacher_Table": syntax error (code 1): , while compiling: create tableTeacher_Table( LT TEXT PRIMARY KEY, Teacher_Name TEXT, Status TEXT, StartTime TEXT, EndTime TEXT)
      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.SQLiteStatement.<init>(SQLiteStatement.java:31)
      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
      at com.practice.testlistviewcustomadapter.DataBaseHelper.onCreate(DataBaseHelper.java:40)
      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
      at com.practice.testlistviewcustomadapter.DataBaseHelper.insertData(DataBaseHelper.java:52)
      at com.practice.testlistviewcustomadapter.CustomAdapter$1.onClick(CustomAdapter.java:122)
      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)
      06-06 20:31:56.716 1593-2079/system_process W/ActivityManager:   Force finishing activity com.practice.testlistviewcustomadapter/.MainActivity
06-06 20:31:56.754 1299-1355/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

1 个答案:

答案 0 :(得分:0)

您在Create Table查询中缺少空格和逗号:

将其更改为:

String query = "create table " + TABLE_NAME +"(LT TEXT PRIMARY 
KEY,Teacher_Name TEXT, Status TEXT, StartTime TEXT, EndTime TEXT);";
db.execSQL(query);