从SQLite转换为MySQL

时间:2011-11-08 07:23:09

标签: mysql sqlite

我在SQLite数据库中创建的这个表:

CREATE TABLE [tickets] (
[id] INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER  NULL,
[size] FLOAT  NULL,
[phone] VARCHAR(10)  NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT  NULL
);

现在INSERT操作是:

INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');

现在我在MySQL中创建了同一个表:

CREATE TABLE tickets (
id INTEGER  PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER  NULL,
size FLOAT  NULL,
phone VARCHAR(10)  NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT  NULL
);
MySQL的

INSERT操作是:

INSERT INTO tickets VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO tickets VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO tickets VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');

当我插入这些值时,我收到了一个错误: - there can be only one TIMESTAMP column with current_timestamp in default of on update clause

...但我无法将所有这些值插入MySQL。帮帮我?

3 个答案:

答案 0 :(得分:1)

在SQLite中你有两列

[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,

在MySQL上你只有一个

date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,

并且您正尝试在其上插入两个值...

你应该试试

INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)

答案 1 :(得分:1)

乍一看,您的varchar列的大小为10,但是您要插入大于10的数据。确保您的varchar列的宽度足以容纳您的数据。

答案 2 :(得分:0)

您的MySQL架构似乎不符合您要插入的内容。

摘自这篇文章:Should I use field 'datetime' or 'timestamp'? ... MySQL中的时间戳通常用于跟踪记录的更改,并在每次更改记录时更新。如果要存储特定值,则应使用日期时间字段。

将您的MySQL架构更改为更接近:

...

  • phone VARCHAR(12)NULL,
  • date DATE DEFAULT CURRENT_DATE NULL,
  • time TIME DEFAULT CURRENT_TIME NULL,

...