2014-04-19 55 views
-1

我試圖將數據插入到具有自動遞增主鍵的表中。 我的表結構是這樣的:如何在Java中執行SQL插入

int:PK 
varchar:Item_ID 
varchar:Item_name 
int:FK_type 

當我嘗試了事先準備好的聲明:

prep = conn.prepareStatement("insert into Inventory values (?, ?, ?);"); 

我得到這個錯誤:

Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (table Inventory has 4 columns but 3 values were supplied) 
    at org.sqlite.DB.newSQLException(DB.java:383) 
    at org.sqlite.DB.newSQLException(DB.java:387) 
    at org.sqlite.DB.throwex(DB.java:374) 
    at org.sqlite.NativeDB.prepare(Native Method) 
    at org.sqlite.DB.prepare(DB.java:123) 
    at org.sqlite.PrepStmt.<init>(PrepStmt.java:42) 
    at org.sqlite.Conn.prepareStatement(Conn.java:404) 
    at org.sqlite.Conn.prepareStatement(Conn.java:399) 
    at org.sqlite.Conn.prepareStatement(Conn.java:383) 
    at org.checkout.CheckInventory.<init>(CheckInventory.java:58) 
    at org.checkout.main.main(main.java:18) 

當我嘗試執行語句:

stat.executeUpdate("insert into Inventory (Item_ID, Item_name, FK_type) values ('TP1', 'NX Tripod 1', 2)"); 

我得到這個錯誤:

java.sql.SQLException: [SQLITE_MISUSE] Library used incorrectly (out of memory) 
    at org.sqlite.DB.newSQLException(DB.java:383) 
    at org.sqlite.DB.newSQLException(DB.java:387) 
    at org.sqlite.DB.throwex(DB.java:374) 
    at org.sqlite.NativeDB._exec(Native Method) 
    at org.sqlite.Stmt.executeUpdate(Stmt.java:152) 
    at org.checkout.CheckInventory.insertItem(CheckInventory.java:73) 
    at org.checkout.CheckInventory$1.actionPerformed(CheckInventory.java:25) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

我在做什麼錯了?

編輯:

我得到一個[SQLITE_MISUSE]錯誤,當我嘗試在聲明中添加四列。

這裏是輸入數據

protected static void insertItem() throws Exception { 

    stat.execute("insert into Inventory (Item_ID, Item_name, FK_type) values ('TP1', 'NX Tripod 1', 2)"); 


} 
+0

似乎是相同的表現:https://bitbucket.org/xerial/sqlite-jdbc/issue/51/javasqlsqlexception-sqlite_misuse-library –

+0

請發佈您用於插入數據的完整代碼。 –

回答

1

你的表有四列,你的刀片只插入三個值的方法。嘗試從表格中刪除一列,或者插入一行時插入四個值。