tags:

views:

425

answers:

5

This question is based on this thread.

I run unsuccessfully

sudo mysql 
\. /users/cs/SO_db/posts.sql

I get the error

ERROR 1146 (42S02): Table 'personal.posts' doesn't exist

MySQL's manual says

A five-character SQLSTATE value ('42S02'). The values are specified by ANSI SQL and ODBC and are more standardized. Not all MySQL error numbers are mapped to SQLSTATE error codes. The value 'HY000' (general error) is used for unmapped errors.

and

Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE)

Message: Table '%s.%s' doesn't exist

How can you solve the error message?

A: 

Have you actually created the database 'personal' and the table 'posts'?

You might want to try something like:

mysql -h localhost -u <user> -p<password> -D personal < /users/cs/SO_db/posts.sql
Nick Brosnahan
A: 

Your posts.sql contains some statements referencing a posts table, which doesn't exist in your personal schema.

To "solve" the error, ensure the table is created!

Paul Dixon
+2  A: 

The SQL script you have loaded makes reference to a database and/or table which does not exist in the database.

Typically one would not call the mysql tool with sudo, as the system user privileges are different from MySQL users.

To execute an SQL script through mysql I would try something like:

cat somefile.sql | mysql -u <mysqluser> -p <mysqldb>

This command would load 'somefile.sql' into mysql tool, connecting to a MySQL server on localhost as user <mysqluser> and selecting the database <mysqldb>. The mysql tool will prompt for <mysqluser>'s access password before executing the script.

nathan
A: 

probably your sql file doesn't include the "create table" command.

dam
+2  A: 

As I mentioned in the post you referenced, you NEED to create the tables first. Peek at the XML or the SQL output on what columns you need. e.g. here is a table that can hold the output from badges.xml (I don't have the others available right now..)

CREATE TABLE `badges` (
  `Id` int(11) NOT NULL default '0',
  `UserId` int(11) not NULL,
  `Date` datetime not  NULL,
  `Name` varchar(32) not NULL,
  PRIMARY KEY  (`Id`),
  KEY `Date` (`Date`),
  KEY `UserId` (`UserId`)
) ;
nos
Thank you for your answer! --- You can run a SQL command according to MySQL manuals by `mysql db_name < script.sql > output.file`.
Masi
I have the table `badges` in my MyQSL database `test`. **How can you run the Python code at http://stackoverflow.com/questions/1146470/to-read-sos-data-dump-effectively/1147259#1147259 such that it inserts the data directly to MySQL database, not to a text file as it does at the moment?**
Masi
You'd run this "python importscript.py badges.xml | mysql test"You might need other arguments to the mysql command if you need to specify a username/password etc.
nos