我试图将数据插入到数据库中,但是在添加之后,当我检查数据库时,我得到一个空结果,它测量它仍然是空的。我没有得到错误的位置,我做了一些日志,但我没有看到错误。我确定我做了一个愚蠢的错误,但我无法看到它。 此插入不起作用: bdd.insert(TABLE_RSSI,null,values)
PS:我在这段代码中做了一些更改,比如冒号的ather名称,我添加了两个clauns,但在代码完全运行之前。 这个代码我使用:
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);
}
}
public class GrillePuissanceBDD {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "puissancesOffLine.db";
private static final String TABLE_RSSI = "table_rssi";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_X = "Xcoordinate ";
private static final int NUM_COL_X = 1;
private static final String COL_Y = "Ycoordinate";
private static final int NUM_COL_Y = 2;
private static final String COL_SSID = "SSID";
private static final int NUM_COL_SSID = 3;
private static final String COL_RSSI = "RSSI";
private static final int NUM_COL_RSSI = 4;
private SQLiteDatabase bdd;
private MaBaseSQLite mabaseSQLite;
public GrillePuissanceBDD (Context context)
{ mabaseSQLite = new MaBaseSQLite (context , NOM_BDD ,null ,VERSION_BDD);
Log.i("creation base","creation");
}
public void open()
{
bdd = mabaseSQLite.getWritableDatabase();
}
public void close()
{
bdd.close();
}
public SQLiteDatabase getBDD()
{
return bdd;
}
public long insertGrille(Grille grille)
{
ContentValues values = new ContentValues();
//values.put(COL_ID, grille.getId());
values.put(COL_X, 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 int updateGrille (int id ,Grille grille)
{ ContentValues values = new ContentValues();
//values.put(COL_ID, grille.getId());
values.put(COL_X, grille.getxCoordinate());
values.put(COL_Y, grille.getyCoordinate());
values.put(COL_SSID, grille.getSsid());
values.put(COL_RSSI, grille.getrssi());
return bdd.update(TABLE_RSSI, values,COL_ID + " = " +id, null );
}
public int removeLivreWithID (int id)
{
return bdd.delete(TABLE_RSSI, COL_ID + " = " +id ,null);
}
public Grille getGrilleWithTitre (String titre)
{
//Cursor c = bdd.query(TABLE_LIVRES, new String [] { COL_ID, COL_ISBN, COL_TITRE}, COL_TITRE + " LIKE \"" + titre + "\"", null, null, null, null);
// Cursor c = bdd.query(TABLE_LIVRES, new String[] {COL_ID, COL_ISBN, COL_TITRE}, COL_TITRE + " LIKE \"" + titre +"\"", null, null, null, null);
Cursor c =bdd.query(TABLE_RSSI, new String[] {COL_X, COL_Y,COL_SSID,COL_RSSI}, COL_ID + " LIKE '" + titre +"'", null, null, null, null);
Log.i("getGrilleWithTitre",titre);
Log.i("Cursor",cursorToPuissance(c)+"");
return cursorToPuissance(c);
}
private Grille cursorToPuissance(Cursor c) {
if (c.getCount()==0)
{Log.i("c.getCount()",c.getCount()+"");
return null;
}
c.moveToFirst();
Grille grille = new Grille();
grille.setId(c.getInt(NUM_COL_ID));
grille.setxCoordinate(c.getString(NUM_COL_X));
grille.setyCoordinate(c.getString(NUM_COL_Y));
grille.setSsid(c.getString(NUM_COL_SSID));
grille.setrssi(c.getString(NUM_COL_RSSI));
Log.i("c.getString(NUM_COL_RSSI)",c.getString(NUM_COL_RSSI)+"");
c.close();
return grille;
}
}
public class Grille{
private int id;
private String xCoordinate;
private String yCoordinate;
private String ssid;
private String rssi;
public Grille ()
{
}
public Grille(String xCoordinate, String yCoordinate,String rssi, String ssid )
{
this.ssid = ssid;
this.xCoordinate = xCoordinate ;
this.yCoordinate = yCoordinate ;
this.rssi = rssi ;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getSsid()
{
return ssid;
}
public void setSsid(String ssid)
{
this.ssid = ssid;
}
public String getxCoordinate()
{
return xCoordinate;
}
public void setxCoordinate(String xCoordinate)
{
this.xCoordinate = xCoordinate;
}
public String getyCoordinate()
{
return yCoordinate;
}
public void setyCoordinate(String yCoordinate)
{
this.yCoordinate = yCoordinate;
}
public String getrssi()
{
return rssi;
}
public void setrssi(String rssi)
{
this.rssi = rssi;
}
public String toString()
{
return "ID :"+id+"\nSSID : "+ssid+"\nX : "+xCoordinate+"\nY : "+yCoordinate+"\nPuissance : "+rssi ;
}
}
public class Add extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void Ajouter(View view)
{
EditText edittext1 = (EditText) findViewById(R.id.editText1) ;
String xCoordinate = edittext1.getText().toString();
EditText edittext2 = (EditText) findViewById(R.id.editText2) ;
String yCoordinate = edittext2.getText().toString();
EditText edittext3 = (EditText) findViewById(R.id.editText3) ;
String ssid = edittext3.getText().toString();
EditText edittext4 = (EditText) findViewById(R.id.editText4) ;
String rssi = edittext4.getText().toString();
xCoordinate = "xCoordinate";
yCoordinate = "yCoordinate";
ssid = "ssid";
rssi = "rssi";
switch (view.getId()){
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;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是日志:
04-16 03:46:35.660: I/creation base(814): creation
04-16 03:46:35.730: I/insertion(814): Ycoordinate=yCoordinate RSSI=ssid Xcoordinate =xCoordinate SSID=rssi
04-16 03:46:35.730: I/insertion(814): xCoordinate
04-16 03:46:35.750: I/insertion(814): 3
04-16 03:46:35.801: I/grille***(814): ID :0
04-16 03:46:35.801: I/grille***(814): SSID : rssi
04-16 03:46:35.801: I/grille***(814): X : xCoordinate
04-16 03:46:35.801: I/grille***(814): Y : yCoordinate
04-16 03:46:35.801: I/grille***(814): Puissance : ssid
04-16 03:46:35.801: I/getxCoordinate***(814): xCoordinate
04-16 03:46:35.820: I/getGrilleWithTitre(814): xCoordinate
04-16 03:46:35.820: I/c.getCount()(814): 0
04-16 03:46:35.820: I/Cursor(814): null
04-16 03:46:35.830: I/c.getCount()(814): 0
04-16 03:46:35.830: I/livreFromBdd(814): null
谢谢。