douqin6785 2012-10-16 01:34
浏览 26

如何从数据库中获取原始插入语句[重复]

Possible Duplicate:
Get Insert Statement for existing row in MySQL

Lets say we have a table called users:

CREATE TABLE IF NOT EXISTS users(
UID int(11) PRIMARY KEY NOT NULL auto_increment,
fname varchar(100) default NULL,
lname varchar(100) default NULL,
username varchar(20) default NULL UNIQUE,
password blob   
)ENGINE=InnoDB DEFAULT CHARSET=utf8

Lets assume there's a few rows filled up in the table. I know there is a query that returns the creation of the table -> SHOW CREATE TABLE users But I'm looking to recreate individual insert statements to save them in a log...I know this sounds weird, but I am making a custom CMS where everything is logged, and hopefully where updated/deleted rows could rollback at point in time...therefore I would need to recreate exact insertion query from table data (I have searched all over the web for an answer and can't find it...)

Is there a query that would return "automatically" the query that inserted that specific row by primary key?

I am looking for a query that would do this:

SHOW INSERT FROM users WHERE PRIMARY_KEY=2

Returns:

INSERT INTO users (UID,fname,lname,username,password) VALUES (2,'somename','somelastname','someusername','someAESencryptedPassword')

The reason I'm thinking such a query would exist/be possible is because when you backup a database with php myadmin (cpanel) and you open the file, you can actually view each insert to recreate the table with all rows a that point in time...

  • 写回答

2条回答 默认 最新

  • doupa1883 2012-10-16 01:48
    关注

    There is no such command to retrieve the original insert statement. However, you can always remake the insert statement based on the existent table structure and data.

    The following link may help you, where this has already been asked:

    Get Insert Statement for existing row in MySQL

    Another possible option is using mysqldump with php to export the data as SQL statements.

    评论

报告相同问题?