Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 主键在具有自动增量的SQLLite中不唯一_Java_Sqlite - Fatal编程技术网

Java 主键在具有自动增量的SQLLite中不唯一

Java 主键在具有自动增量的SQLLite中不唯一,java,sqlite,Java,Sqlite,我创建了一个具有自动递增主键的表: String sql = "CREATE TABLE COMPANY " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + " NAME TEXT NOT NULL)" 接下来,我想向数据库中添加一条记录 但是,当我运行程序时,我会收到以下消息: 主键约束失败(唯一约束失败:COMPANY.ID) 你知道如何自动增加一个记录号码吗?我不是在编辑现有记录,而是在添加记录。为了插入公司价值观,我在ID字段中使用了术语“活

我创建了一个具有自动递增主键的表:

String sql = "CREATE TABLE COMPANY " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + " NAME TEXT NOT NULL)" 
接下来,我想向数据库中添加一条记录

但是,当我运行程序时,我会收到以下消息:

主键约束失败(唯一约束失败:COMPANY.ID)

你知道如何自动增加一个记录号码吗?我不是在编辑现有记录,而是在添加记录。为了插入公司价值观,我在ID字段中使用了术语“活动”

public class Main {

    public static void main(String[] args) {
        String nam;
        int ag;
        Scanner key = new Scanner(System.in);
        System.out.println("\nEnter your name : ");
        nam = key.next();
        System.out.println("\nEnter your age : ");
        ag = key.nextInt();


        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            System.out.println("Opened database successfully");

            stmt = c.createStatement();

            String sql = "INSERT INTO COMPANY VALUES(" + active + " ,
            '" + nam + "', " + ag + ")"; 
            stmt.executeUpdate(sql);
            c.commit();

            ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;"
           );
            while ( rs.next() ) {
                int id = rs.getInt("id");
                String  name = rs.getString("name");
                int age  = rs.getInt("age");
                System.out.println( "ID = " + id );
                System.out.println( "NAME = " + name );
                System.out.println( "AGE = " + age );
                System.out.println();
            }
            rs.close();
            stmt.close();
            c.close();
        } catch ( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + 
       e.getMessage() );
            System.exit(0);
        }
        System.out.println("Operation done successfully");
    }
    }

首先,不要输入id。它是由数据库分配的

其次,我非常确定在插入到autoincrement表中时需要显式指定列:

INSERT INTO COMPANY (name, age) VALUES(?, ?)
请注意,CREATETABLE语句与INSERT语句不匹配


最后,请查阅“SQL注入”。使用准备好的语句而不是字符串连接

是否确实指定了自动递增?我的猜测是,您的查询每次都尝试将
NULL
作为主键插入(这是SQLite允许的),从而导致主键重复。您可以转储数据库以查看它中的实际内容我的查询是:String sql=“CREATE TABLE COMPANY”+“(ID INTEGER主键自动递增,+“NAME TEXT NOT NULL)”//这可以编译记录,但不能将记录添加到现有数据库中。谢谢您的评论。我在id的值中插入NULL,并添加自动创建的记录编号。我正在阅读“SDQL注入”来学习准备好的语句。