Golang操作mysql创建表格的问题

func createTable(db *sql.DB, table string) {
    sql := `
DROP TABLE IF EXISTS ` + table + `;
CREATE TABLE ` + table + `(
    uid INT(10) NOT NULL AUTO_INCREMENT,
    username VARCHAR(64) NULL DEFAULT NULL,
    departname VARCHAR(64) NULL DEFAULT NULL,
    created DATE NULL DEFAULT NULL,
    PRIMARY KEY(uid)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;`
    fmt.Println("\n" + sql + "\n")
    smt, err := db.Prepare(sql)
    checkErr(err)
    smt.Exec()
}
打印调试信息:
[ `run` | done: 14.339562ms ]
    DROP TABLE IF EXISTS tablea;
    CREATE TABLE tablea(
        uid INT(10) NOT NULL AUTO_INCREMENT,
        username VARCHAR(64) NULL DEFAULT NULL,
        departname VARCHAR(64) NULL DEFAULT NULL,
        created DATE NULL DEFAULT NULL,
        PRIMARY KEY(uid)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;


    panic: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE tablea(
        uid INT(10) NOT NULL AUTO_INCREMENT,
        username VARCHAR(64)' at line 2

共 2 个回复


zykzhang

我把打印出来的SQL语句粘贴到terminal里可以成功执行的.

# 0

chideat

DROPCREATE 分成两条语句试试呢

# 1