Novak007
Novak007

Reputation: 436

Inserting into MySQL table using a variable

I have long known that one could fetch results from a mysql table and print it as :

(MySQL C API)

MYSQL_RES *result = mysql_store_result(con);

if (result == NULL)    
{    
  finish_with_error(con);    
}

int num_fields = mysql_num_fields(result);        
MYSQL_ROW row;        
while ((row = mysql_fetch_row(result)))    
{     
   printf("%s ", row[i] ? row[i] : "NULL");    
}

Now suppose I have to copy a database into another, can I do the other way round i.e can I somehow use the row variable to insert into the new database.

If I could do this copying a database would be so easy.

By copying a database I mean I have to create a database that is exactly the same as the original one, with all its data and attributes being the same.

Upvotes: 0

Views: 283

Answers (1)

horsefeathers
horsefeathers

Reputation: 116

I think prepared statements/PDO should work for what you're looking for. Without knowing what your table schema looks like, it's a bit hard to tell you what the statement would look like, but here's some information for the C API.

http://dev.mysql.com/doc/refman/5.1/en/c-api-prepared-statements.html http://dev.mysql.com/doc/refman/5.0/en/c-api-prepared-statement-data-structures.html http://dev.mysql.com/doc/refman/5.1/en/sql-syntax-prepared-statements.html

Upvotes: 1

Related Questions