程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SQLite中的多个唯一列大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决sqlite中的多个唯一列?

开发过程中遇到sqlite中的多个唯一列的问题如何解决?下面主要结合日常开发的经验,给出你关于sqlite中的多个唯一列的解决方法建议,希望对你解决sqlite中的多个唯一列有所启发或帮助;

我不确定是什么问题。它在这里工作正常:

import sqlite3

# connect to memory-only database for tesTing
con = sqlite3.connect('')
cur = con.cursor()

# create the table
cur.execute('''
create table CorpWalletJournal (
    datE int, refID INT, refTypEID INT, ownername1 TEXT, 
    ownerid1 int, ownername2 TEXT, ownerid2 int, argname1 TEXT, 
    argID1 ID, @R_673_10929@nt INT, balancE int, reason TEXT, accountKey INT, 
    UNIQUE (ownerID1, ownerID2, accountKey, argID1)
);
''')
con.commit()

insert_sql = '''INSERT INTO CorpWalletJournal 
(date, refID, refTypEID, ownername1, ownerID1, ownername2, ownerID2, 
argname1, argID1, @R_673_10929@nt, balance, reason, accountKey)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'''

## create 5 rows changing only argID1 - it works:
for argID in xrange(5): 
    cur.execute(insert_sql, (1, 1, 1, 'a', 1, 'a', 1, 'a', argID, 1, 1, 'a', 1))
con.commit()

# Now try to insert a row that is already there:
cur.execute(insert_sql,  (1, 1, 1, 'a', 1, 'a', 1, 'a', 0, 1, 1, 'a', 1))

我从最后一行得到的错误是:

TraceBACk (most recent call last):
  file "teststdio.py", line 41, in <module>
    cur.execute(insert_sql,  (1, 1, 1, 'a', 1, 'a', 1, 'a', 0, 1, 1, 'a', 1))
sqlite3.IntegrityError: columns ownerID1, ownerID2, accountKey, argID1 
    are not unique

解决方法

我试图创建一个表,我需要它不允许3个字段相同的行。

当我使用sqLLite在Python中创建表时,使用了以下代码,但几乎没有得到任何结果。它通常在写入2条记录后停止,因此显然可以相信它是重复的。

create table CorpWalletJournal (
    datE int,refID INT,refTypEID INT,ownerName1 TEXT,ownerid1 int,ownerName2 TEXT,ownerid2 int,argName1 TEXT,argID1 ID,@R_673_10929@nt INT,balancE int,reason TEXT,accountKey INT,UNIQUE (ownerID1,ownerID2,accountKey,argID1)
);

因此,我希望数据库不允许在ownerID1,ownerID2,accountKey和argID1相同的记录。

谁能帮我这个忙吗?

谢谢!

大佬总结

以上是大佬教程为你收集整理的SQLite中的多个唯一列全部内容,希望文章能够帮你解决SQLite中的多个唯一列所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。