2013-07-28 107 views
0

我有一張有74列的表格,我想插入數據。有沒有辦法在該表中只插入5列並將剩下的值留空?該列不是爲了要麼,它可能是Prepared Statement for tons of columns

ps.setInt(1, 1); 
    ps.setInt(7, "Steve"); 
    ps.setInt(10, time); 

等....

我必須填寫的數據爲每列?

表的結構:

CREATE TABLE IF NOT EXISTS `vbulletin_user` (
    `userid` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `usergroupid` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `membergroupids` char(250) NOT NULL DEFAULT '', 
    `displaygroupid` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `username` varchar(100) NOT NULL DEFAULT '', 
    `password` char(32) NOT NULL DEFAULT '', 
    `passworddate` date NOT NULL DEFAULT '0000-00-00', 
    `email` char(100) NOT NULL DEFAULT '', 
    `styleid` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `parentemail` char(50) NOT NULL DEFAULT '', 
    `homepage` char(100) NOT NULL DEFAULT '', 
    `icq` char(20) NOT NULL DEFAULT '', 
    `aim` char(20) NOT NULL DEFAULT '', 
    `yahoo` char(32) NOT NULL DEFAULT '', 
    `msn` char(100) NOT NULL DEFAULT '', 
    `skype` char(32) NOT NULL DEFAULT '', 
    `showvbcode` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `showbirthday` smallint(5) unsigned NOT NULL DEFAULT '2', 
    `usertitle` char(250) NOT NULL DEFAULT '', 
    `customtitle` smallint(6) NOT NULL DEFAULT '0', 
    `joindate` int(10) unsigned NOT NULL DEFAULT '0', 
    `daysprune` smallint(6) NOT NULL DEFAULT '0', 
    `lastvisit` int(10) unsigned NOT NULL DEFAULT '0', 
    `lastactivity` int(10) unsigned NOT NULL DEFAULT '0', 
    `lastpost` int(10) unsigned NOT NULL DEFAULT '0', 
    `lastpostid` int(10) unsigned NOT NULL DEFAULT '0', 
    `posts` int(10) unsigned NOT NULL DEFAULT '0', 
    `reputation` int(11) NOT NULL DEFAULT '10', 
    `reputationlevelid` int(10) unsigned NOT NULL DEFAULT '1', 
    `timezoneoffset` char(4) NOT NULL DEFAULT '', 
    `pmpopup` smallint(6) NOT NULL DEFAULT '0', 
    `avatarid` smallint(6) NOT NULL DEFAULT '0', 
    `avatarrevision` int(10) unsigned NOT NULL DEFAULT '0', 
    `profilepicrevision` int(10) unsigned NOT NULL DEFAULT '0', 
    `sigpicrevision` int(10) unsigned NOT NULL DEFAULT '0', 
    `options` int(10) unsigned NOT NULL DEFAULT '33570831', 
    `birthday` char(10) NOT NULL DEFAULT '', 
    `birthday_search` date NOT NULL DEFAULT '0000-00-00', 
    `maxposts` smallint(6) NOT NULL DEFAULT '-1', 
    `startofweek` smallint(6) NOT NULL DEFAULT '1', 
    `ipaddress` char(15) NOT NULL DEFAULT '', 
    `referrerid` int(10) unsigned NOT NULL DEFAULT '0', 
    `languageid` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `emailstamp` int(10) unsigned NOT NULL DEFAULT '0', 
    `threadedmode` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `autosubscribe` smallint(6) NOT NULL DEFAULT '-1', 
    `pmtotal` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `pmunread` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `salt` char(30) NOT NULL DEFAULT '', 
    `ipoints` int(10) unsigned NOT NULL DEFAULT '0', 
    `infractions` int(10) unsigned NOT NULL DEFAULT '0', 
    `warnings` int(10) unsigned NOT NULL DEFAULT '0', 
    `infractiongroupids` varchar(255) NOT NULL DEFAULT '', 
    `infractiongroupid` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `adminoptions` int(10) unsigned NOT NULL DEFAULT '0', 
    `profilevisits` int(10) unsigned NOT NULL DEFAULT '0', 
    `friendcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `friendreqcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `vmunreadcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `vmmoderatedcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `socgroupinvitecount` int(10) unsigned NOT NULL DEFAULT '0', 
    `socgroupreqcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `pcunreadcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `pcmoderatedcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `gmmoderatedcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `assetposthash` varchar(32) NOT NULL DEFAULT '', 
    `fbuserid` varchar(255) NOT NULL DEFAULT '', 
    `fbjoindate` int(10) unsigned NOT NULL DEFAULT '0', 
    `fbname` varchar(255) NOT NULL DEFAULT '', 
    `logintype` enum('vb','fb') NOT NULL DEFAULT 'vb', 
    `fbaccesstoken` varchar(255) NOT NULL DEFAULT '', 
    `newrepcount` smallint(5) unsigned NOT NULL DEFAULT '0', 
    `bloggroupreqcount` int(10) unsigned NOT NULL DEFAULT '0', 
    `showblogcss` int(11) NOT NULL DEFAULT '1', 
    PRIMARY KEY (`userid`), 
    KEY `usergroupid` (`usergroupid`), 
    KEY `username` (`username`), 
    KEY `birthday` (`birthday`,`showbirthday`), 
    KEY `birthday_search` (`birthday_search`), 
    KEY `referrerid` (`referrerid`), 
    KEY `fbuserid` (`fbuserid`), 
    KEY `email` (`email`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; 
+0

你的意思是'74'列嗎?您應該首先考慮正常化表格。 74太多了。 –

+0

是的,表中有74條數據。 – Tyluur

+0

這些字段在數據庫設計中是否爲「可空」? –

回答

1

由於Luiggi門多薩的評論指出,查詢將正常工作。
例如,在此查詢:

ps=conn.prepareStatement("INSERT INTO vbulletin_user(userid,username,password,email) VALUES(?,?,?,?)"); 
ps.setInt(1,1); 
ps.setString(2,"scott"); 
ps.setString(3,"tiger"); 
ps.setString(4,"[email protected]"); 

的默認值就可以插入其他列,因爲它們是在架構提及。
,你應該爲Date的默認值設置爲某個具體日期,而不是0000-00-00就像你passworddatebirthday_search列,因爲它會導致some exception when accessing by java

java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date 


這裏是Fiddle,在此我只是改變了default date的值爲passworddatebirthday_search列。

+0

-1:在我的評論中我說*這將工作,如果你的表的列支持NULL值*,但如果你讀**表DML,幾乎所有的字段都有'NOT NULL' 。下次在發佈代碼之前測試代碼作爲答案(順便說一下,我在[sqlfiddle](http://sqlfiddle.com/#!9/f6592/1))上測試了這個代碼。 –

+0

@LuiggiMendoza,我在安裝在我的系統上的mysql上測試過它,它工作正常。所有'NOT NULL'字段在模式中都有'DEFAULT'值,在未提供值的情況下將使用該值。在sqlfiddle中,你沒有看到這個錯誤,在MySQL的查詢面板中不允許DDL和DML語句;只允許SELECT語句。將DDL和DML放入架構面板中。 –

+0

是的,對不起,我幾乎在凌晨3點在我的國家發佈了這條評論。我重新測試它,發現它工作(日期字段除外)。 Downvote刪除。 –

相關問題