2016-03-02 63 views
0

我想創建與我面臨着一些issues.Here多個column.But表是我的代碼:如何修復android sdk中的表創建錯誤?


    package com.edkul.vimal.edkul; 

    import android.database.DatabaseErrorHandler; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.database.Cursor; 
    import android.content.Context; 
    import android.content.ContentValues; 

    public class DatabaseHandler extends SQLiteOpenHelper { 

     private static final int DATABASE_VERSION = 1; 
     private static final String DATABASE_NAME = "studentRecord.db"; 
     private static final String TABLE_STUDENT = "StudentRecords"; 
     private static final String COLUMN_ID = "_id"; 
     private static final String COLUMN_STUDENTNAME = "StudentName"; 
     private static final String COLUMN_EMAIL = "StudentEmail"; 
     private static final String COLUMN_PASSWORD = "StudentPassword"; 
     private static final String COLUMN_CPASSWORD = "StudentConfirmPassword"; 
     private static final String COLUMN_INSTITUTE = "InstituteName"; 
     private static final String COLUMN_CONTACTNO = "ContactInfo"; 

     public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
      super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
     } 

     public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { 
      super(context, name, factory, version, errorHandler); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      String query = "CREATE TABLE " + TABLE_STUDENT + " (" + 
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + 
        COLUMN_STUDENTNAME + " TEXT " + 
        COLUMN_EMAIL + " TEXT " + 
        COLUMN_PASSWORD + " TEXT " + 
        COLUMN_CPASSWORD + " TEXT " + 
        COLUMN_INSTITUTE + " TEXT " + 
        COLUMN_CONTACTNO + " TEXT " + 
        ") "; 
      db.execSQL(query); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS" + TABLE_STUDENT); 
      onCreate(db); 
     } 

     //add new row to the database 
     public void addStudent(StudentRecord studentRecord){ 
      ContentValues values = new ContentValues(); 
      values.put(COLUMN_STUDENTNAME,studentRecord.get_Name()); 
      values.put(COLUMN_EMAIL,studentRecord.get_Email()); 
      values.put(COLUMN_PASSWORD,studentRecord.get_Password()); 
      values.put(COLUMN_CPASSWORD,studentRecord.get_Cpassword()); 
      values.put(COLUMN_INSTITUTE,studentRecord.get_Institute()); 
      values.put(COLUMN_CONTACTNO,studentRecord.get_Contact()); 
      SQLiteDatabase db = getWritableDatabase(); 
      db.insert(TABLE_STUDENT, null, values); 
      db.close(); 
     } 

     //Delete the student record from the table 

     public void deleteStudent(String studentName){ 
      SQLiteDatabase db = getWritableDatabase(); 
      db.execSQL("DELETE FROM " + TABLE_STUDENT + "WHERE" + COLUMN_STUDENTNAME + "=\" "+ studentName + "\";"); 
     } 

     // print the database as a string 

     public String databaseToString(){ 
      String dbString = ""; 
      SQLiteDatabase db = getWritableDatabase(); 
      String query = "SELECT * FROM " + TABLE_STUDENT + "WHERE 1" ; 
      Cursor c = db.rawQuery(query,null); 
      c.moveToFirst(); 
      while (!c.isAfterLast()) { 
       if (c.getString(c.getColumnIndex("studentName")) != null){ 
        dbString += c.getString(c.getColumnIndex("studentName")); 
        dbString += "\n"; 
       } 
      } 
      db.close(); 
      return dbString; 
     } 
    } 
com.edkul.vimal.edkul E/SQLiteLog: (1) near "Records": syntax error 
com.edkul.vimal.edkul E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.edkul.vimal.edkul, PID: 1992 android.database.sqlite.SQLiteException: near "Records": syntax error (code 1): , while compiling: CREATE TABLE Student Records(_id INTEGER PRIMARY KEY AUTOINCREMENT Student Name TEXT Student Email TEXT Student Password TEXT Student Confirm Password TEXT Institute Name TEXT Contact Info TEXT) 

我想創建表6 column.But我不能由於上述錯誤而導致進入數據庫。

回答

1

表名表的列名不應該包含空間。因此,請刪除空格或用下劃線替換它(_)。

如果要保留空間,請將名稱用雙引號(")包裝。例如:

create table "abc xyz" ("first name" string); 

編輯:query字符串應該是有點這樣的:

String query = "CREATE TABLE " + TABLE_STUDENT + " (" + 
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        COLUMN_STUDENTNAME + " TEXT, " + 
        COLUMN_EMAIL + " TEXT, " + 
        COLUMN_PASSWORD + " TEXT, " + 
        COLUMN_CPASSWORD + " TEXT, " + 
        COLUMN_INSTITUTE + " TEXT, " + 
        COLUMN_CONTACTNO + " TEXT" + 
        ")"; 
+0

謝謝,我跟着指示,但現在我得到以下錯誤 近「StudentName」:語法錯誤 03-02 22:35:51.176 15700-15700/com.edkul.vimal.edkul E/AndroidRuntime:致命例外:主要 進程:com.edkul.vimal.edkul,PID:15700 android.da tabase.sqlite.SQLiteException:near「StudentName」:語法錯誤(代碼1):編譯時:CREATE TABLE StudentRecords(_id INTEGER PRIMARY KEY AUTOINCREMENT StudentName –

+0

'StudentRecords'和'('之間應該有*空格*。此外,列名應該用逗號分隔(',')。 –

+0

如何以及在哪裏逗號。我對此很陌生,沒有任何想法。我編輯了我的代碼並插入了空間,但是請你給我建議。 –