更新数据库后更改我的程序?

Oli*_*man 2 java mysql sql sql-insert

对不起,如果我的问题不具体或者之前已经回答过.我试着寻找它并提出更好的方式,但这是最准确的方法.

我用Java开发了一个程序,我用以下方式在数据库中插入一个新行:

INSERT INTO table_name VALUES (?,?,?)

问题是我在程序的许多部分都有这个查询,现在我决定在我的表中添加第四列.我是否必须在程序中使用新问号更新EVERY SINGLE查询?如果我不这样做,它会崩溃.

在这些情况下,最好的方法是什么?

Joh*_*Woo 5

是.

你需要添加额外的?(参数占位符),因为你正在使用implicit INSERT语句.这意味着您没有指定要插入值的表的列名.

INSERT INTO table_name VALUES (?,?,?)
// the server assumes that you are inserting values for all
// columns in your table
// if you fail to add value on one column. an exception will be thrown
Run Code Online (Sandbox Code Playgroud)

下次创建INSERT语句时,请确保在其上指定列名,以便在通过添加额外列更改表时,不会更新所有占位符.

INSERT INTO table_name (Col1, col2, col3) VALUES (?,?,?)
// the server knows that you are inserting values for a specific column
Run Code Online (Sandbox Code Playgroud)