java sql插入,如何在Java中执行SQL插入

博主在尝试向 Inventory 表中插入数据时遇到了两个错误。第一个错误是使用 PreparedStatement 时,由于提供了三个值而表有四个列导致的。第二个错误是在尝试执行带有四个值的插入语句时遇到了内存使用不当的问题。解决方法包括检查插入语句是否包含所有列的值,或者调整表结构以匹配提供的值。
摘要由CSDN通过智能技术生成

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

When I try the prepared statement:

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

I get this Error:

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.(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.(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)");

I get this error:

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)

What am I doing wrong?

Edit:

I get a [SQLITE_MISUSE] error when I try to add four columns in the statement.

Here is the method that inputs the data

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值