如何在sqlSave()命令中跳过主键? [英] How to skip primary key in sqlSave() command?

查看:144
本文介绍了如何在sqlSave()命令中跳过主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RODBC在MySQL数据库中插入data.frame.我正在使用的命令如下:

I am trying to insert a data.frame in the MySQL database using RODBC. The command I am using is the following:

sqlSave(channel,dbData,tablename='table_name', append=TRUE,safer=TRUE,fast=FALSE,verbose=TRUE)

现在我要在其中插入数据的表具有一个主键,该键是自动递增的.我的表共有7列,包括主键.在我的数据框中,我有6列,因为我不想自己插入PK.但是,当我运行命令时,出现以下错误:

Now the table in which I am trying to insert the data has a primary key which is auto-increment. My table has total of 7 columns including the primary key. In my data frame, I have 6 columns because I don't want to insert the PK myself. However when I run the command, I get the following error:

23000 1062 [MySQL][ODBC 5.1 Driver][mysqld-5.5.13]Duplicate entry '1' for key 'PRIMARY'

从上述错误中,我了解到,当已有记录以1作为其PK时,它正在尝试插入"1"作为主键.知道如何使用sqlSave()避免这种情况吗?

From the above error, I understand that it is trying to insert '1' as the primary key when there is already a record with 1 as its PK. Any idea how I can avoid this using sqlSave()?

谢谢.

推荐答案

您可以尝试在数据框中添加第七列,并为PK列数据设置NULL0的值.然后,MySQL将自动为其生成值.

You can try adding seventh column in your dataframe, and assing value of NULL or 0 for PK column data. Then MySQL will generate value for it automatically.

这篇关于如何在sqlSave()命令中跳过主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆