在SQLite数据库的错误插入 [英] Error inserting in SQLite database

查看:184
本文介绍了在SQLite数据库的错误插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建以下数据库:

I'm trying to create the following database:

public static final String KEY_NAME = "nombre";
public static final String KEY_ROWID = "_id";
public static final String KEY_ID = "id";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

 private static final String DATABASE_CREATE =
        "create table capas (_id integer primary key autoincrement, "
                            + "id text not null, "
                            + "nombre text not null);"
        + "create table poi (_id integer primary key autoincrement, "
                            + "id text not null, "
                            + "nombre text not null, "
                            + "lon real not null, " 
                            + "lat real not null, "
                            + "info text, "
                            + "email text, "
                            + "telefono text, "
                            + "web text, "
                            + "usuario text, "
                            + "capas_id text not null);"
        + "create table mensajes (_id integer primary key autoincrement, "
                            + "id text not null, "
                            + "mensaje text not null, "
                            + "fecha text not null, "
                            + "poi_id text not null, "
                            + "usuario text not null);";

private static final String DATABASE_NAME = "upv_db";
private static final String DATABASE_TABLE_CAPAS = "capas";
private static final String DATABASE_TABLE_POIS = "poi"; 
private static final String DATABASE_TABLE_MENSAJES = "mensajes"; 
private static final int DATABASE_VERSION = 2;

目前正处在这三个表。这些都是初始化,创建和检索从第一个表行的方法(我没有写尚未对其他2表的方法):

There're three tables in it. These are the methods to initialize, create and retrieve rows from the first table (I haven't written the methods for the other 2 tables yet):

   public long createCapa(String id, String nombre) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ID, id);
        initialValues.put(KEY_NAME, nombre);

        return mDb.insert(DATABASE_TABLE_CAPAS, null, initialValues);
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor leerCapas() {

        return mDb.query(DATABASE_TABLE_CAPAS, new String[] {KEY_ROWID, KEY_ID, KEY_NAME}, null, null, null, null, null);
    }

    //Inicializa la tabla de capas por defecto
    public void inicializaCapas(){
        createCapa("cap001", "Escuelas");
        createCapa("cap002", "Departamentos");
        createCapa("cap003", "Edificios");
    }

该应用程序无法正常工作,因为它在数据库中插入时获得一个错误。这是日志中的错误:

The app is not working because it gets an error when inserting in the DB. This is the error in the log:

07-23 20:39:14.354: ERROR/Database(743): Error inserting nombre=Escuelas id=cap001
07-23 20:39:14.354: ERROR/Database(743): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
07-23 20:39:14.354: ERROR/Database(743):     at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
07-23 20:39:14.354: ERROR/Database(743):     at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
07-23 20:39:14.354: ERROR/Database(743):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
07-23 20:39:14.354: ERROR/Database(743):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.upvar.DatosDB.createCapa(DatosDB.java:131)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.upvar.DatosDB.inicializaCapas(DatosDB.java:180)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.upvar.menu.listaCapas(menu.java:52)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.upvar.menu.onCreate(menu.java:46)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-23 20:39:14.354: ERROR/Database(743):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 20:39:14.354: ERROR/Database(743):     at android.os.Looper.loop(Looper.java:123)
07-23 20:39:14.354: ERROR/Database(743):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-23 20:39:14.354: ERROR/Database(743):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 20:39:14.354: ERROR/Database(743):     at java.lang.reflect.Method.invoke(Method.java:521)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-23 20:39:14.354: ERROR/Database(743):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-23 20:39:14.354: ERROR/Database(743):     at dalvik.system.NativeStart.main(Native Method)
07-23 20:39:14.394: ERROR/Database(743): Error inserting nombre=Departamentos id=cap002
.
.
. 

所以,抱怨一些约束冲突,但我看不到任何这些。唯一的限制是主键,它们被设置为自动增量,所以我不知道它是什么抱怨。

So it complains about some constraint violation, but I don't see any of those. The only constraints are the primary keys and they are set to be autoincrement, so I don't understand what it is complaining about.

推荐答案

我试着在这2天,我AP preciate他们所有的建议。但是,你知道吗?当我今天开始的Eclipse,我运行的应用程序,我只是工作了。我不知道怎么样,因为昨天当我完成尝试没有成功。但出乎意料的是它是固定的。

I tried all your suggestions during these 2 days and I appreciate them. But you know what? When I started Eclipse today, I run the app and I just worked out. I have no idea how, because yesterday when I finished trying it didn't work. But surprisingly it's fixed.

我很高兴这件事,而且还担心,因为这让我感觉应用程序是不可靠的。做这些事通常发生在Android或SQLite中?它是否发生在你身上过吗?如果你有一些建议,请分享,因为也许它会停止工作,突然有些一天,谁知道?

I'm quite happy about that, but also concerned, because that makes me feel the app is not reliable. Does these things happen usually in Android or in SQLite? Has it happened to you before? If you have some advice, please share it, because maybe it will stop working suddenly some other day, who knows?

这篇关于在SQLite数据库的错误插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆