使用“插入查询”不会将数据插入到Sqlite数据库中

时间:2017-10-11 14:58:15

标签: java android sqlite android-sqlite

我一直在尝试将字符串数组中的一些项添加到数据库中,我似乎无法使用插入查询来处理我所看到的here我已经尝试了插入查询的各种变体

这是我的代码:

SQLiteDatabase db1 = openOrCreateDatabase( "Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
    try{
        String query = "CREATE TABLE IF NOT EXISTS Station ("
                + "Station_name VARCHAR);";
        db1.execSQL(query);
        Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show();
        for(i=0;i<10;i++)
        {
            Toast.makeText(MainActivity.this, stations[i][0],Toast.LENGTH_SHORT).show();
            query = "INSERT  INTO Station VALUES ('"+stations[i][0]+"');";
            db1.execSQL(query);

        }
    }catch (Exception e){
        Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show();
    }
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null);

    String[] station_array = new String[data_fetch.getCount()];
    data_fetch.moveToFirst();
    i = 0;
    while (data_fetch.moveToNext()) {
        String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
        station_array[i] = name;
        i++;
        Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show();
    }
    data_fetch.close();
}

当我为检索到的数据干杯时,吐司说retrieved datanull 我甚至尝试插入一个字符串,而不是变量,但我仍然得到datanull作为吐司。

任何帮助将不胜感激。

PS这是我试图插入的字符串数组:

stations[0][0]= "New York";
    stations[1][0]= "Boston";
    stations[2][0]= "Las Vegas";
    stations[3][0]= "Miami";
    stations[4][0]= "Chicago";
    stations[5][0]= "New England";
    stations[6][0]= "Detroit";
    stations[7][0]= "Michigan";
    stations[8][0]= "Austin";
    stations[9][0]= "New Orealns";

2 个答案:

答案 0 :(得分:1)

在插入查询中的表名后面的代码中缺少列名:

更正插入查询:

INSERT INTO Station Station_name VALUES ('"+stations[i][0]+"');

示例:

INSERT INTO table_name_here (column1, column2, column3) VALUES ("Learn PHP", "John Poul", NOW());

答案 1 :(得分:0)

我认为这个问题来源于使用二维数组: -

stations[0][0]= "New York";
stations[1][0]= "Boston";
stations[2][0]= "Las Vegas";
stations[3][0]= "Miami";
stations[4][0]= "Chicago";
stations[5][0]= "New England";
stations[6][0]= "Detroit";
stations[7][0]= "Michigan";
stations[8][0]= "Austin";
stations[9][0]= "New Orealns";

而是使用: -

    String[] stations = new String[10];
    stations[0]= "New York";
    stations[1]= "Boston";
    stations[2]= "Las Vegas";
    stations[3]= "Miami";
    stations[4]= "Chicago";
    stations[5]= "New England";
    stations[6]= "Detroit";
    stations[7]= "Michigan";
    stations[8]= "Austin";
    stations[9]= "New Orealns";

或(可能是这个,因为它更灵活

    String[] stations = new String[] {
            "New York",
            "Boston",
            "Las Vegas",
            "Miami", "Chicago",
            "New England",
            "Detroit",
            "Michigan",
            "Austin", 
            "New Orleans"
    };

以及: -

    for(i=0;i< stations.length();i++)
        {
            Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show();
            query = "INSERT  INTO Station VALUES ('"+stations[i]+"');";
            db1.execSQL(query);

        }

或者,查询可以包括列名称,并且是: -

query = "INSERT  INTO Station Station_name VALUES ('"+stations[i]+"');";

您的代码的完整版本可能是: -

    String[] stations = new String[] {
            "New York",
            "Boston",
            "Las Vegas",
            "Miami", "Chicago",
            "New England",
            "Detroit",
            "Michigan",
            "Austin",
            "New Orleans"
    };

    int i=0;
    SQLiteDatabase db1 = openOrCreateDatabase( "Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
    try{
        String query = "CREATE TABLE IF NOT EXISTS Station ("
                + "Station_name VARCHAR);";
        db1.execSQL(query);
        Log.d("STATION_TBLCRT","Table Created");
        //Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show();
        for(i=0;i<stations.length;i++)
        {
            Log.d("STATION_INS","Inserting station " + stations[i]);
            //Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show();
            query = "INSERT  INTO Station VALUES ('"+stations[i]+"');";
            db1.execSQL(query);

        }
    }catch (Exception e){
        Log.d("STATION_INSERR","Error inserting station " + stations[i]);
        //Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show();
    }
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null);

    String[] station_array = new String[data_fetch.getCount()];
    //data_fetch.moveToFirst();
    i = 0;
    while (data_fetch.moveToNext()) {
        String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
        station_array[i] = name;
        Log.d("STATION_GET","Retrieved station " + station_array[i]);
        i++;
        //Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show();
    }
    data_fetch.close();
}

注意!

  • Toast已被Log
  • 取代
  • data_fetch.moveToFirst已被注释掉,因为这会导致跳过第一行。
  • 如果以上操作不止一次,则会添加其他工作站组。
  • data_fetch 中的i ++在报告工作站后已移至(否则数据将始终为空)。
  • 由于Toast的持续时间而使用Toasts可能会产生误导,因此使用Log可以看到所有结果。

示例输出: -

10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_TBLCRT: Table Created
10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New York
10-14 08:16:37.451 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Boston
10-14 08:16:37.454 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Las Vegas
10-14 08:16:37.457 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Miami
10-14 08:16:37.461 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Chicago
10-14 08:16:37.465 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New England
10-14 08:16:37.468 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Detroit
10-14 08:16:37.471 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Michigan
10-14 08:16:37.473 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Austin
10-14 08:16:37.478 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New Orleans
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New York
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Boston
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Las Vegas
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Miami
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Chicago
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New England
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Detroit
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Michigan
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Austin
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New Orleans