2013-11-29 59 views
0

我試圖在SQLite中插入大數據,如微調選定項目,日期時間和字符串。當我運行我得到的應用程序:如何在Android中的SQLite中插入大數據

SQLite異常:沒有這樣的表,而編譯插入。數據庫

我不知道該怎麼辦。

這裏是數據庫代碼

public class DataBase_Adapter 
{ 

     //Database NAme 
     static final String DATABASE_NAME = "lead_management.db"; 

     //Database Version 
     static final int DATABASE_VERSION = 1; 


     // Variable to hold the database instance 
     public SQLiteDatabase db; 

     // Context of the application using the database. 
     private final Context context; 

     // Database open/upgrade helper 
     private DataBaseHelper dbHelper; 

     public DataBase_Adapter(Context _context) 
     { 
      context = _context; 
      dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     public DataBase_Adapter open() throws SQLException 
     { 
      db = dbHelper.getWritableDatabase(); 
      return this; 
     } 

     public void close() 
     { 
      db.close(); 
     } 

     public SQLiteDatabase getDatabaseInstance() 
     { 
      return db; 
     } 

     //Table name 
    static final String TABLE_NEW_LEAD="new_lead"; 

     //Creating New Lead Table Columns 
     private static final String KEY_NEW_LEAD_ID ="id"; 
     private static final String KEY_NEW_LEAD_NAME ="name"; 
     private static final String KEY_NEW_LEAD_EMAIL ="email"; 
     private static final String KEY_NEW_LEAD_MOBILE="mobile"; 
     private static final String KEY_NEW_LEAD_Product="define_products"; 
     private static final String KEY_NEW_LEAD_BUDGET="budget"; 
     private static final String KEY_NEW_LEAD_PRIORITY="priority"; 
     private static final String KEY_NEW_LEAD_STATUS="status"; 
     private static final String KEY_NEW_LEAD_NOTES="notes"; 
     private static final String KEY_NEW_LEAD_REMINDER_DATE="reminder_date"; 
     private static final String KEY_NEW_LEAD_REMINDER_TIME="reminder_time"; 
     private static final String KEY_NEW_LEAD_ADDtoCONTACTS="add_to_contacts"; 


     //// SQL Statement to create a New Lead Database. 
     static final String CREATE_NEW_LEAD_TABLE = "CREATE TABLE"+TABLE_NEW_LEAD+"("+KEY_NEW_LEAD_ID+"integer primary key autoincrement,"+KEY_NEW_LEAD_NAME+"TEXT,"+KEY_NEW_LEAD_EMAIL+"TEXT,"+ KEY_NEW_LEAD_MOBILE+"TEXT,"+KEY_NEW_LEAD_Product+"TEXT,"+KEY_NEW_LEAD_BUDGET+"TEXT,"+KEY_NEW_LEAD_PRIORITY+"TEXT,"+KEY_NEW_LEAD_STATUS+"TEXT,"+KEY_NEW_LEAD_NOTES+"TEXT,"+KEY_NEW_LEAD_REMINDER_DATE+"TEXT,"+KEY_NEW_LEAD_REMINDER_TIME +"TEXT,"+KEY_NEW_LEAD_ADDtoCONTACTS+"TEXT,"+")"; 

     //Insert New Record In New Lead Table 

     public void insert_NewLead_Entry(New_Lead_BeanClass newLead_BeanClass) 
     { 
      SQLiteDatabase sdb = dbHelper.getWritableDatabase(); 

      ContentValues contentNewLead_Val=new ContentValues(); 
      contentNewLead_Val.put(KEY_NEW_LEAD_NAME, newLead_BeanClass.get_Name()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_EMAIL, newLead_BeanClass.get_Email()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_MOBILE, newLead_BeanClass.get_MobileNo()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_Product, newLead_BeanClass.get_Product()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_BUDGET, newLead_BeanClass.get_Budget()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_PRIORITY, newLead_BeanClass.get_Priority()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_STATUS, newLead_BeanClass.get_Status()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_NOTES, newLead_BeanClass.get_Notes()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_DATE, newLead_BeanClass.get_Reminder_Date()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_REMINDER_TIME, newLead_BeanClass.get_Reminder_Time()); 
      contentNewLead_Val.put(KEY_NEW_LEAD_ADDtoCONTACTS, newLead_BeanClass.get_AddtoContact()); 

      sdb.insert(TABLE_NEW_LEAD , null , contentNewLead_Val); 

      //Close The Database Connection 
      sdb.close(); 

     } 

} 

此數據庫Helper類

public class DataBaseHelper extends SQLiteOpenHelper 
    { 

    public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
     { 
        super(context, name, factory, version); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase _db) 
     { 
       _db.execSQL(DataBase_Adapter.CREATE_NEW_LEAD_TABLE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
     { 
        Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " 
          +_newVersion + ", which will destroy all old data"); 
       _db.execSQL("DROP TABLE IF EXISTS " + DataBase_Adapter.TABLE_NEW_LEAD); 

       onCreate(_db); 
     } 

    } 




     /******************************Submit Detail Button***********************************************/ 

     dbHandller=new DataBase_Adapter(this); 
     dbHandller=dbHandller.open(); 

     btn_NewLead_Submit=(Button)findViewById(R.id.newLead_buttonSubmit); 
     btn_NewLead_Submit.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) 
      { 
       // TODO Auto-generated method stub 


       strContactName =eText_newLeadName.getText().toString().trim(); 
       strContactEmail =eText_newLeadEmail.getText().toString().trim(); 
       strContactMobile =eText_newLeadMobile.getText().toString().trim(); 
       strBudget  =eText_newLeadBudget.getText().toString().trim(); 
       strNotes   =eText_newLeadNotes.getText().toString().trim(); 
       strDate   =eText_newLeadDate.getText().toString().trim(); 
       strTime   =eText_newLeadTime.getText().toString().trim(); 

       New_Lead_BeanClass new_Lead_BeanClass = new New_Lead_BeanClass(); 

       new_Lead_BeanClass.set_Name(strContactName); 
       new_Lead_BeanClass.set_Email(strContactEmail); 
       new_Lead_BeanClass.set_MobileNo(strContactMobile); 
       new_Lead_BeanClass.set_Product(selectedProductItem); 
       new_Lead_BeanClass.set_Budget(strBudget); 
       new_Lead_BeanClass.set_Priority(selectedPriorityItem); 
       new_Lead_BeanClass.set_Status(selectedStatusItem); 
       new_Lead_BeanClass.set_Notes(strNotes); 
       new_Lead_BeanClass.set_Reminder_Date(strDate); 
       new_Lead_BeanClass.set_Reminder_Time(strTime); 
       new_Lead_BeanClass.set_AddtoContact(strToggleVlaue); 

       dbHandller.insert_NewLead_Entry(new_Lead_BeanClass); 

       System.out.println("Data Inserted InTO Sqlite DataBase"); 

      } 
     }); 


    } 
     /******************************Load Spinner Data***********************************************/ 
     private void loadSpinnerData() 
     { 
       // database handler 
       DbHelper db = new DbHelper(getApplicationContext()); 

       // Spinner Drop down elements 
       List<String> lables = db.getAllLabels(); 

       // Creating adapter for spinner 
       ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, lables); 

       // Drop down layout style - list view with radio button 
       dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

       // attaching data adapter to spinner 
       spinner_Products.setAdapter(dataAdapter); 

     } 

這裏是我的日誌貓堆棧跟蹤

11-29 11:21:03.431: E/Database(16864): Error inserting budget=89999999 reminder_time=11 : 18 add_to_contacts=true status=closed [email protected] priority=medium name=jersey define_products=jaguar reminder_date=1-1-2014 notes=Tmg technology where message gateway technology mobile=1122334455 
11-29 11:21:03.431: E/Database(16864): android.database.sqlite.SQLiteException: no such table: new_lead: , while compiling: INSERT INTO new_lead(budget, reminder_time, add_to_contacts, status, email, priority, name, define_products, reminder_date, notes, mobile) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569) 
11-29 11:21:03.431: E/Database(16864): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426) 
11-29 11:21:03.431: E/Database(16864): at com.lead_management_project.DataBase_Adapter.insert_NewLead_Entry(DataBase_Adapter.java:188) 
11-29 11:21:03.431: E/Database(16864): at com.lead_management_project.New_Lead_Activity5$9.onClick(New_Lead_Activity5.java:380) 
11-29 11:21:03.431: E/Database(16864): at android.view.View.performClick(View.java:2485) 
11-29 11:21:03.431: E/Database(16864): at android.view.View$PerformClick.run(View.java:9080) 
11-29 11:21:03.431: E/Database(16864): at android.os.Handler.handleCallback(Handler.java:587) 
11-29 11:21:03.431: E/Database(16864): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-29 11:21:03.431: E/Database(16864): at android.os.Looper.loop(Looper.java:123) 
11-29 11:21:03.431: E/Database(16864): at android.app.ActivityThread.main(ActivityThread.java:3683) 
11-29 11:21:03.431: E/Database(16864): at java.lang.reflect.Method.invokeNative(Native Method) 
11-29 11:21:03.431: E/Database(16864): at java.lang.reflect.Method.invoke(Method.java:507) 
11-29 11:21:03.431: E/Database(16864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
11-29 11:21:03.431: E/Database(16864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
11-29 11:21:03.431: E/Database(16864): at dalvik.system.NativeStart.main(Native Method) 
+0

如果在初始數據庫創建後創建/更新/修改了表,則需要更新數據庫版本號。更新版本號,重新編譯並重試。 – drunkenRabbit

+0

表尚未創建,因爲您尚未在變量名和變量名之間添加空格。嘗試像這樣..CREATE_USERNAMEPASSWORD_TABLE =「CREATE TABLE」+ TABLE_UsernamePassword +「(」+ USERNAMEPASSWORD_ID +「INTEGER PRIMARY KEY AUTOINCREMENT,」+ USERNAMEPASSWORD_KEY_WEBTITLE +「TEXT」+ USERNAMEPASSWORD_KEY_WEBPASSWORD +「TEXT」+ USERNAMEPASSWORD_KEY_WEBNAME +「TEXT」+ USERNAMEPASSWORD_KEY_WEBURL + 「TEXT」,+ USERNAMEPASSWORD_KEY_WEBTYPE +「TEXT」,+ USERNAMEPASSWORD_KEY_WEBIMAGE +「INTEGER」+「)」; – Giridharan

+0

@drunkenRabbit我更新數據庫版本號= 4.並且數據插入正確。非常感謝您。 – tazeenmulani

回答

2

嘗試這樣的.. 改變你的靜態最後字符串CREATE_NEW_LEAD_TABLE。

CREATE_USERNAMEPASSWORD_TABLE = "CREATE TABLE "+ TABLE_UsernamePassword + "(" 
+ USERNAMEPASSWORD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
+ USERNAMEPASSWORD_KEY_WEBTITLE + " TEXT," 
+ USERNAMEPASSWORD_KEY_WEBPASSWORD + " TEXT," 
+ USERNAMEPASSWORD_KEY_WEBNAME+" TEXT," 
+ USERNAMEPASSWORD_KEY_WEBURL +" TEXT," 
+ USERNAMEPASSWORD_KEY_WEBTYPE + " TEXT," 
+ USERNAMEPASSWORD_KEY_WEBIMAGE + " INTEGER"+");"; 
+0

仍然我得到了相同的錯誤 – tazeenmulani

+0

是你在模擬器上還是在設備上運行應用程序? –

+0

在模擬器上運行應用程序。 – tazeenmulani

相關問題