gpt4 book ai didi

android - Sqlite 列 id 不是唯一的

转载 作者:搜寻专家 更新时间:2023-11-01 08:02:11 25 4
gpt4 key购买 nike

下面是我的代码,我只是想将我插入到 sqlite 数据库中的数据显示到 TextView 中,但是我有这个错误,指出 id 不是唯一的。

    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 代码。谢谢:)

最佳答案

嗯,我认为这个项目在您第一次运行时运行良好。当您第二次运行它时,它会出错,因为 id 是您的 primary key 并且 id 为 1 的行已经插入到您的数据库中。

要消除错误:

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

关于android - Sqlite 列 id 不是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com