SQLite的列ID不是唯一 [英] Sqlite column id is not unique

查看:383
本文介绍了SQLite的列ID不是唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code我只是想显示我插在SQLite数据库到一个文本视图中的数据,但我有这个错误说的ID不是唯一的。

below is my code i just want to display the data i inserted in the sqlite database to a text view but i have this error saying that the id is not unique.

    package com.example.sqlitetest;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

    TextView scrName, fName;
    Button go;
    SQLiteDatabase db;
    String screenName, fullName;
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = openOrCreateDatabase("MyDataBase", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE if not exists MyTable(ScreenName Varchar, FullName Varchar, id int(3) PRIMARY KEY);");
        db.execSQL("Insert into MyTable VALUES('joff', 'tiquez', 1);");
        cursor = db.rawQuery("Select * from MyTable", null);
        cursor.moveToFirst();

        scrName = (TextView) findViewById(R.id.scrName);
        fName = (TextView) findViewById(R.id.fName);

        while(cursor.isAfterLast() == false){
            //retrieving the data from sqlite (im not sure if im doing it right)
            String screenname = cursor.getString(cursor.getColumnIndex("ScreenName"));
            String fullname = cursor.getString(cursor.getColumnIndex("FullName"));

            //this are the textview
            scrName.setText(screenname);
            fName.setText(fullname);

            cursor.moveToNext();
        }
        db.close();
    }
}

这是我整个的java code。感谢:)

this is my whole java code. thanks :)

推荐答案

嗯,我认为当你运行它第一次这个项目工作正常。当你运行它第二次提示错误,因为 ID 主键和id为1的行已已插入到数据库中。

Well I think this project works fine when you run it first time. When you run it second time it gives error because id is your primary key and the row with the id 1 has been already inserted to your database.

为了摆脱错误之一:

1) uninstall the app and run it again   
2) Don't make id as primary key   
3) Catch the exception and handle it yourself

这篇关于SQLite的列ID不是唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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