2013-02-22 42 views
-2

我在嘗試創建數據庫時,此錯誤: 錯誤信息 輸入代碼在這裏 2月2日至23日:08:46.780:E /數據庫(32721):錯誤 插入電子郵件= af contact = 38 dob = 1/8/68 name = ex 02-23 02:08:46.780:E/Database(32721): android.database.sqlite.SQLiteException:no such table: profiles:,while編譯:INSERT INTO配置文件 (email,contact,dob,name)VALUES(?,?,?,?); 02:02:08:46.780:E/Database(32721): atandroid.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 02-23 02:08:46.780:E/Database(32721):atSQLite數據庫錯誤:找不到表格的Android

package a.vaccination; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class datahandler { 

public static final String KEY_ROWID = "id"; 
public static final String KEY_name = "name"; 
public static final String KEY_dob = "dob"; 
public static final String KEY_contact = "contact"; 
public static final String KEY_email = "email"; 
private static final String TAG = "DBAdapter"; 

private static final String DATABASE_NAME = "profile"; 
private static final String DATABASE_TABLE = "profiles"; 
private static final int DATABASE_VERSION = 2; 

private static final String DATABASE_CREATE 
= "create table (id integer primary  keyautoincrement, " 
+ "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR);"; 
private final Context context; 

private DatabaseHelper DBHelper; 
private SQLiteDatabase db; 

public datahandler(Context ctx) { 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

protected static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     try { 
      db.execSQL(DATABASE_CREATE); 
      //db = DBHelper.getWritableDatabase(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
    + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS contacts"); 
     onCreate(db); 
    } 
    } 

// ---opens the database--- 
public datahandler open() throws SQLException { 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 

// ---closes the database--- 
public void close() { 
    DBHelper.close(); 
} 

// ---insert a record into the database--- 
public long insertRecord(String name, String dob, String contact, 
     String email) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_name, name); 
    initialValues.put(KEY_dob, dob); 
    initialValues.put(KEY_contact, contact); 
    initialValues.put(KEY_email, email); 
    return db.insert(DATABASE_TABLE, null, initialValues); 
} 

// ---updates a record--- 
public boolean updateRecord(long rowId, String name, String dob, 
     String contact, String email) { 
    ContentValues args = new ContentValues(); 
    args.put(KEY_name, name); 
    args.put(KEY_dob, dob); 
    args.put(KEY_contact, contact); 
    args.put(KEY_email, email); 
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
} 
} 
+1

發佈您的代碼。 – 323go 2013-02-22 20:44:50

+0

也附加我的數據庫文件: – Saurabh 2013-02-22 20:45:29

+0

@Saurabh,請盡你最大的努力發佈乾淨的問題,具有良好的格式和最好的英語知識。我現在解決了你的問題。另外,正如323go所說,發佈一些代碼。 – 2013-02-22 21:59:44

回答

1

您的查詢的一個原因是嘗試incorrent:

  • 你缺少的tablename
  • 你缺少鍵和自動增量

所以正確的它之間的空格之後:

String DATABASE_CREATE = "create table TableName(id integer primary key autoincrement, " 
+ "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR)"; 

現在,它會工作。

+0

@ sajmon_d感謝您的幫助,但我仍然面臨error02-24 23:56:04.939:電子數據庫(8203) = contact = dob = name = – Saurabh 2013-02-24 18:41:43

+0

在這裏添加您要插入的完整logcat和Java代碼。 – Sajmon 2013-02-24 19:22:28

0

請點擊這裏表名

private static final String DATABASE_CREATE 
= "create table TABLENAME (id integer primary key autoincrement, " 
+ "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR);"; 

這看起來像背後

no such table: profiles

+0

@ sajmon_d和@ mayukh R感謝您的幫助,但我仍然面臨error02-24 23:56 :電子數據庫(8203):錯誤插入電子郵件=聯繫人= dob =名稱= – Saurabh 2013-02-24 18:43:48