2013-03-26 30 views
0

我正在爲android編寫一個應用程序。我需要讀取一個txt文件並將數據寫入SQLite文件中。我設法完成了所有的代碼,但是它應該將值插入到數據庫中的部分不起作用。我給出了以下代碼:什麼不是這個代碼插入到SQLite數據庫的值?

try{ 
    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, firstNumber); // Contact Name 
    values.put(KEY_PH_NO, strfinal); // Contact Phone 
    // Inserting Row 
    db.insert(TABLE_CONTACTS, null, values); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

此代碼未輸入數據庫中的值,並且在操作完成後數據庫保持爲空。這有什麼問題?謝謝。 編輯:好的,這裏是全碼:

public class DatabaseHandler extends SQLiteOpenHelper { 

     // All Static variables 
     // Database Version 
     private static final int DATABASE_VERSION = 1; 

     // Database Name 
     private static final String DATABASE_NAME = "feedsmanager.sqlite"; 

     // Contacts table name 
     private static final String TABLE_CONTACTS = "table_to_hold_all_values"; 

     // Contacts Table Columns names 
     private static final String KEY_ID = "id"; 
     private static final String KEY_NAME = "name";//ctid 
     private static final String KEY_PH_NO = "phone_number";//feedname,address;feedname;address etc 
     Context ctx; 

     public DatabaseHandler(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      ctx=context; 
     } 

     // Creating Tables 
     @Override 
     public void onCreate(SQLiteDatabase db) { 

      //Log.d("in onCreate","twitch1"); 
      String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" 
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," 
        + KEY_PH_NO + " TEXT);"; 

      //Log.d("below createcontacts","twitch1"); 
      try{ 
      db.execSQL(CREATE_CONTACTS_TABLE); 
      }catch(Exception e){Log.d("create went wrong: "+e,"twitch11");} 
      //Log.d("below db.execsql","twitch1"); 
      populate(db); 
    } 

      void populate(SQLiteDatabase dbs){ 
      String line=""; 


      try{ 
      InputStream is = ctx.getAssets().open("feedtitlesandaddresses.txt"); 
      InputStreamReader iz=new InputStreamReader(is); 
      BufferedReader br = new BufferedReader(iz); 
      //SQLiteDatabase dbs = this.getWritableDatabase(); 
      while((line=br.readLine())!=null) { 
       //Log.d("fcked here6","twitch1"); 
       StringTokenizer stringTokenizer = new StringTokenizer(line, "<"); 

       String firstNumber=""; 
       String strfinal=""; 
        firstNumber = (String) stringTokenizer.nextElement(); 
        **//Calculations to give values to firstNumber and strfinal excluded 

        ContentValues values = new ContentValues(); 

        values.put(KEY_NAME, firstNumber); // Contact Name 
        values.put(KEY_PH_NO, strfinal); // Contact Phone 
        // Inserting Row 

        dbs.insert(TABLE_CONTACTS, null, values);} 

       dbs.close();}catch(Exception e){Log.d("yeah error is"+e,"twitch12");} 
     }} 
+0

你的logcat出現錯誤嗎?請張貼它。 – span 2013-03-26 21:14:51

+0

@span不!我把它放在try catch塊中,沒有錯誤! – 2013-03-26 21:15:55

+0

@ user2213396:顯示你的'try/catch' – jlordo 2013-03-26 21:16:57

回答

0

嘗試把this.db.insert(TABLE_CONTACTS, null, values);。因爲幾個月前我有同樣的問題,那就是解決方案。

+0

當我按照你所說的做時,它在編譯器中給出錯誤:「db無法解析或者不是字段。」 – 2013-03-26 21:25:31

+0

發佈該類的所有代碼可能會有用。 – mare23 2013-03-26 21:32:18

+0

增加了完整的代碼 – 2013-03-26 22:02:50

0

而不是db.insert()...使用db.insertOrThrow(),可能是一些約束錯誤。

+0

試過了,沒有工作。 – 2013-03-26 21:31:19

0

什麼是你的db對象?是SQLiteDatabase db = this.getWriteableDatabase();?我看到有些人使用getReadAbleDatabase()而不是getWriteableDatabase()方法。

相關問題