Android:数据库 sqLite 不插入表

2024-03-11

我试图将数据插入数据库,但添加后,当我检查数据库时,我得到一个空结果,这意味着它仍然是空的。我不知道错误在哪里,我做了一些日志,但没有看到错误。我确信我犯了一个愚蠢的错误,但我看不到它。 此插入不起作用: bdd.insert(TABLE_RSSI, null, 值)

PS:我对此代码做了一些更改,例如冒号的另一个名称,并且添加了两个子句,但在代码完美运行之前。这是我正在使用的代码:

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

谢谢。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:数据库 sqLite 不插入表 的相关文章

随机推荐

  • CodeIgniter DB 会话问题:sess_expire_on_close

    当 CI 的会话存储在数据库中时 我遇到了一个非常奇怪的问题 我使用会话来存储有关用户是否登录我的网站的信息 由于某种原因 我的一个来自立陶宛的朋友 我提到了这个国家 以防它以某种方式相关 无法登录 当我监视会话表时 她似乎正在为她访问的每
  • Ruby 可以与 r 交互吗?

    一位朋友需要为她的博士学位做一些 R 编程 由于我是一名程序员 所以请我帮助她 所以我看了一些r http www r project org related http www programmingr com webstuff http
  • 如何在Spring Boot测试中强制事务提交?

    如何在 Spring Boot 中强制事务提交 使用 Spring Data 运行方法时 and not方法之后 我读过这里应该可以 Transactional propagation Propagation REQUIRES NEW 在另
  • 如何对浮点数和复数进行近似结构模式匹配

    我读过并理解浮点数舍入问题 https docs python org 3 tutorial floatingpoint html例如 gt gt gt sum 0 1 10 1 0 False gt gt gt 1 1 2 2 3 3 F
  • Django - 自定义 403 模板

    我正在尝试在 Django 1 5 中使用我的 403 404 500 自定义模板 404 和 500 工作完美 但 403 仍然向我展示内置的 Django 403 模板 我将所有三个模板都放在项目的根模板目录中 它们被命名为 403 h
  • 无法在使用 Arquillian 和 WildFly 的 JPA 集成测试中注入 EntityManager

    我正在尝试使用以下堆栈进行集成测试 App server Embedded WildFly CDI container Weld Database In memory H2 ORM Hibernate JPA Platform Java 8
  • 哪些 C++ 编译器(如果有)进行尾递归优化?

    在我看来 在 C 和 C 中进行尾递归优化都可以很好地工作 但在调试时我似乎从未看到表明这种优化的帧堆栈 这很好 因为堆栈告诉我递归的深度 不过 优化也会很好 有 C 编译器进行此优化吗 为什么 为什么不 我该如何告诉编译器去做呢 对于 M
  • 我可以通过 JNI 从 Node.js 调用 Java 吗?如何调用?

    我可以通过 Node js 调用 JavaJNI 有例子吗 你应该尝试节点java https github com nearinfinity node javanpm 模块是一个编写良好的 JNI 包装器 Node jave 似乎还没有被
  • Google 地图:自动关闭打开的 InfoWindows?

    在我的网站上 http www uptownelite com test html city dallas tx 我正在使用 Google Maps API v3 在地图上放置房屋标记 除非您明确单击关闭图标 否则 InfoWindows
  • 将 @Embeddable 映射到单独的表中

    两个表如 CREATE TABLE foo id INT PRIMARY KEY x TEXT CREATE TABLE bar foo id INT REFERENCES foo id ON DELETE CASCADE y TEXT z
  • MPI_Send() 和 MPI_Ssend() 之间的区别?

    I know MPI Send 是一个阻塞调用 它会等待直到可以安全地修改应用程序缓冲区以供重用 为了使发送调用同步 应该与接收者握手 我们需要使用MPI Ssend 我想知道两者之间的区别 假设我需要在进程之间发送固定数量的字节 哪一个应
  • 函数式编程中的无限循环?

    我想知道 在函数式编程中可以完成无限循环吗 例子 使用windows API获取windows消息时 通常是循环实现的 我知道可以创建一个无限期递归的函数 我预计这会导致堆栈溢出 对于函数式编程来说 无限循环是错误的思维模式吗 是操作系统接
  • 如何从 JavaScript 中的 URL 获取基域

    我想从 javascript 中的 url 中提取基本域 例如 对于下面列出的网址列表 我需要获取谷歌网站 or google co in视情况而定 作为结果 www google comwww google co inwww images
  • 循环向量(R 中的自省?)或其他方法

    我有一张桌子tf带有列标题的值formant vowel length IL SG 这就是我获取它们的值的方式 f1a lt subset tf tf vowel a tf formant F1 IL f2a lt subset tf tf
  • 如何使用 docker-compose 更新现有镜像?

    我有多个微服务 并且正在使用 docker compose 进行开发部署 当微服务代码库发生一些变化时 我会触发 ci 作业来重新部署它们 我有下面的脚本来执行此操作 但每次我都必须从头开始构建所有图像 然后运行它们 完成所有这些操作后 我
  • SQLAlchemy 类型对象“日期”没有属性“_set_parent_with_dispatch”

    我正在使用 sqlalchemy 和简单模型模式 class Mail Base tablename mail id Column Integer primary key True date Column Date nullable Fal
  • 使用 ggplot 将图例添加到单折线图

    我只是尝试制作一个折线图并使用 R 中的 ggplot 添加图例 以下是我的代码 ggplot mtcars aes x mpg y wt geom line stat identity scale fill identity name g
  • Visual Studio 2010 - 无法从 GAC 添加程序集引用

    我现在已经转到 Visual Studio 2010 Beta 2 我已经使用了几天 但现在我无法再添加来自 GAC 的参考文献 http bildr no view 549966 http bildr no view 549966 抱歉
  • 从 getaddrinfo() 获取服务器 ip 0.0.0.0:0

    我正在遵循 Beej 的 NP 指南 我做了一些修改 并尝试通过 getaddrinfo 获取我的服务器程序的 IP 原文可以在这里找到http beej us guide bgnet output html singlepage bgne
  • Android:数据库 sqLite 不插入表

    我试图将数据插入数据库 但添加后 当我检查数据库时 我得到一个空结果 这意味着它仍然是空的 我不知道错误在哪里 我做了一些日志 但没有看到错误 我确信我犯了一个愚蠢的错误 但我看不到它 此插入不起作用 bdd insert TABLE RS