Android:SQLite在数据库中插入

时间:2014-04-16 09:51:24

标签: android sqlite

我无法插入数据库,这段代码有什么问题。谢谢。

public long insertGrille(Grille grille) {
    ContentValues values = new ContentValues();
    //values.put(COL_ID, grille.getId());
    values.put(COL_X, grille.getxCoordinate());
    Log.i("COL_X, grille.getxCoordinate()", grille.getxCoordinate() + "");
    values.put(COL_Y, grille.getyCoordinate());
    values.put(COL_SSID, grille.getSsid());
    values.put(COL_RSSI, grille.getrssi());
    Log.i("insertion", values + "");
    Log.i("insertion", grille.getxCoordinate() + "");
    Log.i("insertion", bdd.insert(TABLE_RSSI, null, values) + "");
    return bdd.insert(TABLE_RSSI, null, values);
}

创建数据库:

public class MaBaseSQLite extends SQLiteOpenHelper{

private static final String TABLE_RSSI = "table_rssi";
private static final String COL_ID = "ID";
private static final String COL_X = "Xcoordinate ";
private static final String COL_Y = "Ycoordinate ";
private static final String COL_SSID = "SSID";
private static final String COL_RSSI = "RSSI";

private static final String CREATE_BDD = " CREATE TABLE " + " TABLE_RSSI " + " ("   + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_X + " TEXT NOT NULL, " + COL_Y + " TEXT NOT NULL, " + COL_SSID + " TEXT NOT NULL, " + COL_RSSI + " TEXT NOT NULL);";  


public MaBaseSQLite(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_BDD);
    Log.i("base créee","base créee");

}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE" +  TABLE_RSSI + ";");
    onCreate(db);
}

} 的修改

  case R.id.button1:
        Grille grille = new Grille(xCoordinate, yCoordinate, ssid, rssi);

        //livre.setIsbn(isbn);
        //livre.setTitre(titre);
        GrillePuissanceBDD liv = new GrillePuissanceBDD(getApplicationContext());
        liv.open();
        liv.insertGrille(grille);
        Log.i("grille***", grille + "");
        Log.i("getxCoordinate***", grille.getxCoordinate() + "");
        Grille livreFromBdd = liv.getGrilleWithTitre(grille.getxCoordinate());
        Log.i("livreFromBdd", livreFromBdd + "");
        if (livreFromBdd != null) {
            Toast.makeText(this, livreFromBdd.toString(), Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Prbleme", Toast.LENGTH_LONG).show();
        }
        liv.close();
        break;

我没有收到错误,这就是堆栈跟踪:

    04-16 04:47:34.700: I/creation base(9205): creation
04-16 04:47:34.860: I/COL_X, grille.getxCoordinate()(9205): xCoordinate
04-16 04:47:34.880: I/insertion(9205): Ycoordinate=yCoordinate RSSI=ssid Xcoordinate =xCoordinate SSID=rssi
04-16 04:47:34.880: I/insertion(9205): xCoordinate
04-16 04:47:34.990: I/insertion(9205): 29
04-16 04:47:35.020: I/grille***(9205): ID :0
04-16 04:47:35.020: I/grille***(9205): SSID : rssi
04-16 04:47:35.020: I/grille***(9205): X : xCoordinate
04-16 04:47:35.020: I/grille***(9205): Y : yCoordinate
04-16 04:47:35.020: I/grille***(9205): Puissance : ssid
04-16 04:47:35.020: I/getxCoordinate***(9205): xCoordinate
04-16 04:47:35.040: I/Cursor c(9205): android.database.sqlite.SQLiteCursor@41757ca8
04-16 04:47:35.110: I/c.getCount()(9205): 0
04-16 04:47:35.110: I/Cursor(9205): null
04-16 04:47:35.140: I/cursorTest(9205): ID :1
04-16 04:47:35.140: I/cursorTest(9205): SSID : 
04-16 04:47:35.140: I/cursorTest(9205): X : 
04-16 04:47:35.140: I/cursorTest(9205): Y : 
04-16 04:47:35.140: I/cursorTest(9205): Puissance : 
04-16 04:47:35.150: I/c.getCount()(9205): 0
04-16 04:47:35.150: I/livreFromBdd(9205): null

2 个答案:

答案 0 :(得分:2)

我并不是真正理解你所写的内容,而是根据我所读过的内容。

代码中的这些行显示

private static final String COL_X = "Xcoordinate ";
private static final String COL_Y = "Ycoordinate ";

删除末尾的空格,因为您在ContentValues

中使用了相同的字符串

另外,我建议您访问方法open更受限制(private),在open方法中调用insertGrille

答案 1 :(得分:2)

实际上并且确切地知道您的代码究竟出了什么问题,这就是为什么我尝试提供一个简单的工作示例来适应您的情况:)

这是一个关于如何将数据插入数据库的简单示例:

让我们以你的DBHelper为例,我改变它做了一些改变(我将所有私有属性公开)

MaBaseSQLite.java

    public class MaBaseSQLite extends SQLiteOpenHelper{

    public static final String TABLE_RSSI = "table_rssi";
    public static final String COL_ID = "ID";
    public static final String COL_X = "Xcoordinate ";
    public static final String COL_Y = "Ycoordinate ";
    public static final String COL_SSID = "SSID";
    public static final String COL_RSSI = "RSSI";

    private static final String CREATE_BDD = " CREATE TABLE " + " TABLE_RSSI " + " ("   + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_X + " TEXT NOT NULL, " + COL_Y + " TEXT NOT NULL, " + COL_SSID + " TEXT NOT NULL, " + COL_RSSI + " TEXT NOT NULL);";  


    public MaBaseSQLite(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_BDD);
        Log.i("base créee","base créee");

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE" +  TABLE_RSSI + ";");
        onCreate(db);
    }

}

然后让我们创建另一个活动:

MainActivity.java

public class MainActivity extends Activity {

    MaBaseSQLite dbHelper;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //intialisation de DBHelper
        dbHelper = new MaBaseSQLite(this, "MyDB", null, 1);

        //insertion des donnees
        insertGrille("12", "56", "rssi1", "ssid1");
                insertGrille("16", "14", "rssi2", "ssid2");
                insertGrille("10", "13", "rssi3", "ssid3");

        //recuperation des donnees
        getGrilles();

    }

    public void insertGrille(String x, String y, String rssi, String ssid){
        //get Writable DB
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        //Insert Data
        ContentValues values = new ContentValues();

        values.put(MaBaseSQLite.COL_X, x);
        values.put(MaBaseSQLite.COL_Y, y);
        values.put(MaBaseSQLite.COL_RSSI, rssi);
        values.put(MaBaseSQLite.COL_SSID, ssid);

        db.insert(MaBaseSQLite.TABLE_RSSI, null, values);

        Log.i("MyTag", "data inserted");        

    }


    public void getGrilles() {
        //requete de recuperation
        String selectQuery = "SELECT  * FROM " + MaBaseSQLite.TABLE_RSSI;

        //get Readable DB
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows
        if (cursor.moveToFirst()) {
            do {
                //pour le moment je fais rien d'interessant avec ces donnees :)

                Log.i("MyTag", "grille.x= " + cursor.getString(1));
                Log.i("MyTag", "grille.y= " + cursor.getString(2));
                Log.i("MyTag", "grille.rssi= " + cursor.getString(3));
                Log.i("MyTag", "grille.ssid= " + cursor.getString(4));

            } while (cursor.moveToNext());
        }
    }


}

这是对代码的简单测试: enter image description here

et bonne chance avec ton projet bataille navale:D

如果出现问题,请告诉我!祝你好运