如何在Java中执行SQL插入 [英] How to perform an SQL insert in Java
本文介绍了如何在Java中执行SQL插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用自动增量主键将数据插入到表中。
我的表格结构如下:
I am trying to insert data in to a table with an auto increment primary key. My table structure is like this:
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)
当我尝试执行该语句时:
When I try executing the statement:
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]错误。
I get a [SQLITE_MISUSE] error when I try to add four columns in the statement.
以下是输入数据的方法
protected static void insertItem() throws Exception {
stat.execute("insert into Inventory (Item_ID, Item_name, FK_type ) values ('TP1', 'NX Tripod 1', 2)");
}
推荐答案
您的表有四列,插入只插入三个值。尝试从表中删除列,或者在插入行时插入四个值。
Your table has four columns and your inserts are only inserting three values. Try removing a column from the table, or alternatively inserting four values when inserting a row.
这篇关于如何在Java中执行SQL插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文