-1

我正在尝试将数据从黑莓插入 SQLite 数据库。当我调用屏幕时,我还调用了一个方法来创建数据库:

boolean sdCardPresent = false;

public static Database sqliteDB;

URI uri;
public  void createdatabase()
{

    try{
        Enumeration e = FileSystemRegistry.listRoots();
        root = (String)e.nextElement();
        Dialog.inform(root);
        if(root.equalsIgnoreCase("sdcard/"))
        {
        sdCardPresent = true;
        }
    if(!sdCardPresent)
        {
            Dialog.inform("O seu dispositivo nao suporta este compartimento sem cartao de memoria, verifique se existe um SD card no seu dispositivo.");
            UiApplication.getUiApplication().pushScreen(new Tab_Main());

        }else{
         uri = URI.create(
            "file:///SDCard/Databases/MBA.db");
        sqliteDB = DatabaseFactory.openOrCreate(uri);
        sqliteDB = DatabaseFactory.open(uri);
        Statement st = sqliteDB.createStatement("DROP TABLE IF EXISTS atms;CREATE  TABLE atms " +
                "( id INTEGER PRIMARY KEY AUTOINCREMENT ," +
                "  localizacao TEXT, " +
                "  mapa TEXT ," +
                "  foto1 TEXT ," +
                "  foto2 TEXT," +
                "  zona TEXT);" +
                "" +
                "DROP TABLE IF EXISTS balcoes;CREATE  TABLE balcoes " +
                "(  id INTEGER PRIMARY KEY AUTOINCREMENT ," +
                "  localizacao TEXT, " +
                "  mapa TEXT ," +
                "  foto1 TEXT ," +
                "  foto2 TEXT," +
                "  zona TEXT);" +
                "" +
                "DROP TABLE IF EXISTS contactos;CREATE  TABLE contactos " +
                "( id INTEGER PRIMARY KEY AUTOINCREMENT ," +
                "  nome TEXT, " +
                "  numero_conta INTEGER);") ;
           st.prepare();
           st.execute();
           st.close();
           sqliteDB.close();
        Dialog.inform("Status: Database was successfully created.");
        //}
    } catch (Exception e){
        System.out.println(e.getMessage());
        Dialog.inform("\n "+e);
        //UiApplication.getUiApplication().popScreen(Screen);
    }

}

之后,我使用一些 Editfields 和其他组件来获取值。我调用方法来插入参数并发送查询:

  public  void insert_update(String query) {
    try {
                sqliteDB = DatabaseFactory.openOrCreate(uri);
        Statement st = sqliteDB.createStatement(query);                 
        //Statement statement = _db.createStatement(query); 
        st.prepare();                                          
        st.execute();  
        st.close();
        Dialog.inform("Adicionado com sucesso!!!");
    }
    catch ( Exception e ) {         
        Dialog.inform(e+"");
    }
}

例如,当我用它调用它时,query = "insert into contactos values (null,"a","1");我得到一个异常。SQL 逻辑错误或缺少数据库。

4

1 回答 1

0

尝试删除第三列值周围的引号。

INSERT INTO contactos VALUES (null,"a", 1);

它被定义为一个整数。

numero_conta INTEGER

编辑

System.out.println(uri); // check if null, or different
if (uri == null) {
    uri = URI.create(
       "file:///SDCard/Databases/MBA.db");
}
sqliteDB = DatabaseFactory.openOrCreate(uri);
sqliteDB = DatabaseFactory.open(uri); // open again

Statement st = sqliteDB.createStatement(query);
于 2013-06-25T20:53:11.247 回答