使用 Perl SQLite 自动递增
我在使用 Perl 的 SQLite 中自动增量时遇到问题。
数据库设计:
$dbh->do( "CREATE TABLE IF NOT EXISTS `Users` (
`UserID` VARCHAR(45) NOT NULL PRIMARY KEY UNIQUE ,
`First_Name` VARCHAR(45) NOT NULL ,
`Last_Name` VARCHAR(45) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
`Password` VARCHAR(45) NOT NULL )" ) ;
$dbh->do( "CREATE TABLE IF NOT EXISTS `Files` (
`FileID` INTEGER AUTO_INCREMENT ,
`UserID` VARCHAR(45) NOT NULL ,
`File_Name` VARCHAR(45) NOT NULL ,
`File` BLOB NULL ,
PRIMARY KEY (`FileID`, `UserID`) ,
CONSTRAINT `fk_Files_Users1`
FOREIGN KEY (`UserID` )
REFERENCES `Users` (`UserID` )
ON DELETE CASCADE
ON UPDATE CASCADE)" ) ;
查询:
$dbh->do( "INSERT INTO Files ( UserID, File_Name )
VALUES ( '$_[0]', '$_[1]' )" ) ;
尽管我已将 FileID 标识为 AUTO_INCRMENT 和 PRIMARY KEY,但当我使用上述查询添加新字段时,FileID 仍为空并且不会自动递增。有什么想法吗?我的桌子设计错了吗?
干杯,
内特
I am having trouble with auto increment in SQLite using Perl.
Database design:
$dbh->do( "CREATE TABLE IF NOT EXISTS `Users` (
`UserID` VARCHAR(45) NOT NULL PRIMARY KEY UNIQUE ,
`First_Name` VARCHAR(45) NOT NULL ,
`Last_Name` VARCHAR(45) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
`Password` VARCHAR(45) NOT NULL )" ) ;
$dbh->do( "CREATE TABLE IF NOT EXISTS `Files` (
`FileID` INTEGER AUTO_INCREMENT ,
`UserID` VARCHAR(45) NOT NULL ,
`File_Name` VARCHAR(45) NOT NULL ,
`File` BLOB NULL ,
PRIMARY KEY (`FileID`, `UserID`) ,
CONSTRAINT `fk_Files_Users1`
FOREIGN KEY (`UserID` )
REFERENCES `Users` (`UserID` )
ON DELETE CASCADE
ON UPDATE CASCADE)" ) ;
The query:
$dbh->do( "INSERT INTO Files ( UserID, File_Name )
VALUES ( '$_[0]', '$_[1]' )" ) ;
Even though I have identified the FileID as being both an AUTO_INCREMENT and a PRIMARY KEY, when I add a new field using the above query the FileID is left blank and isn't incremented automatically. Any thoughts? Have I designed the tables wrong?
Cheers,
Nate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您在文件表上创建一个仅包含 FileID 字段的主键(您有两个字段)。我认为您有一个拼写错误:它是 AUTOINCRMENT(您输入 AUTO_INCRMENT)。请参阅:http://www.sqlite.org/autoinc.html
I recommend you to create a primary key on Files table with the FileID field exclusively (you have two fields). And I think you have a typo: it's AUTOINCREMENT, (you type AUTO_INCREMENT). See this: http://www.sqlite.org/autoinc.html