【问题标题】:What isn't this code inserting values into SQLite database?这段代码不是将值插入 SQLite 数据库吗?
【发布时间】:2013-03-26 21:12:53
【问题描述】:

我正在为 android 编写一个应用程序。我需要读取一个 txt 文件并将数据写入 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");}
        }}

【问题讨论】:

  • 您的 logcat 中是否出现错误?请张贴。
  • @span 不!我把它放在一个try catch块中,没有错误!
  • @user2213396:显示你的try/catch
  • 那么,你有例外吗?堆栈跟踪说明了什么?您应该能够捕获 sql 异常。
  • 怎么知道数据库是空的?可能读取数据库的那部分代码是错误的。如果你使用模拟器,实际下载数据库并使用 sqlite3 读取它。

标签: java android sqlite


【解决方案1】:

尝试放置this.db.insert(TABLE_CONTACTS, null, values);。因为几个月前我遇到了同样的问题,这就是解决方案。

【讨论】:

  • 当我按照你说的做时,编译器出错:“db 无法解析或不是字段。”
【解决方案2】:

而不是db.insert() ...使用db.insertOrThrow(),可能是一些约束错误。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      相关资源
      最近更新 更多