Simple Syntax Error in SQLite: "Error near line 1: near "SQLite" - sql

I am having a great deal of trouble with this seemingly simple issue. I can't seem to get .read to work. Here is the code that I have tried:
Any help would be greatly appreciated!

.open test.sql
This opens a Sqlite3 database (Creating a new one if the file doesn't already exist) and attaches it as the main one in the sqlite3 shell session.
.read test.sql
This attempts to read a text file full of SQL statements and execute them one by one.
A sqlite database is not a text file full of SQL statements, hence the syntax errors when you try to treat it as one.

The correct syntax is:
CREATE TABLE t (x int);
or
CREATE TABLE t (x INTEGER);
Read more about the CREATE statement and data types.

Related

psql restore sql dump syntax error

I am trying to import https://www.yelp.com/dataset/documentation/sql into a PostgreSQL instance. It is having problems with accent marks/backtick. Other than doing a character replace, is there any other way to deal with this?
ERROR: syntax error at or near "PaxHeader"
LINE 1: PaxHeader/yelp_db.sql17 uid=998889796
^
ERROR: syntax error at or near "`"
LINE 1: CREATE DATABASE /*!32312 IF NOT EXISTS*/ `yelp_db` /*!40100 ...
^
ERROR: syntax error at or near "USE"
LINE 1: USE `yelp_db`;
^
ERROR: syntax error at or near "`"
LINE 1: DROP TABLE IF EXISTS `attribute`;
These are typical MySQL syntax issues that PostgreSQL conforms to the standard on and therefore doesn't support. There are a few different converters on GitHub that might help. When I had to do this last, there were tools to convert text dumps. They didn't work perfectly but they got things close enough. Reviewing the tools around today, they tend to assume you have an actual MySQL database, not just a dump file.
So it looks like the appropriate way to address this today is to load the data into MySQL and then move it to PostgreSQL. In this regard you seem to have four options that I can think of for converting the schema and data:
There are tools to convert XML dumps from MySQL and load these into PostgreSQL.
You could set up a foreign data wrapper from PostgreSQL to the MySQL db and then copy the schemas and data in.
You could manually convert the schemas, and then dump/reload the data using an ETL process via CSV.
There are tools to read a live MySQL database and insert the data into PostgreSQL.

SQL query executed through Windows batch: missing table

I execute a SQL query (for PostgreSQL) via psql.exe inside a Windows batch. I get an error I can't explain, saying that a FROM clause is missing for a table that is not called within the query (see below). When I search in the batch file for geo_c3_0_3_mo table, the string is not found...
Any idea on this kind of issue?
EDIT :
If I copy-paste the query from the batch file into a pgAdminIII SQL query window, the query runs perfectly and no error message is returned.
When I remove one of the subqueries, the error either disappear or mention another badly written table name (for instance: missing FROM-clause for table "geoc__0_3_mo")... It seems more and more that the issue comes from the length of the line (19,413 characters!). To me, it is not possible to write the query on several lines within a batch file, like inside a pgAdminIII SQL query window. The solution would be to keep the query inside a *.sql file and to call that file from the batch file.
Write the query to a tempfile in your batch, then execute it with psql -f. This will bypass command-line length issues.

sqlite3 Error when Running in Terminal

I have a .sql file called create.sql. It consists of the following lines:
drop table if exists Items;
drop table if exists Auctions;
create table Items(...);
create table Auctions(...);
When I run:
sqlite3 test < create.sql
It crashes and says, "Error: near line 1: near "drop": syntax error"
Any thoughts?
I just tried this SQL Fiddle, there is no problem with the SQL syntax
The problem seems to be with the way the file is created and looks to be platform specific
if you are using Notepad++ or other editors, try converting the file to Unix style EOL i.e. \n, the file might be having \r\n which sqllit3 is complaining about.

Create an external table Informix

I am trying to create an external table in Informix.
create external table test_table(cols varchar(10))
using ( datafiles('C:/sample.txt'), format 'delimited', deluxe);
But it gives me an error :
[Error Code: -26174, SQL State: IX000] Incorrect DATAFILE entry C:/sample.txt.
Can anyone suggest me the right syntax for path entry...
I have also tried.
load from "C:\sample.txt" insert into test_table;
Which gives me a syntax error. Any help is greatly appreciated!!
About the create external table
Did you check the correct syntax at the manual? (here)
At datafiles keyword, you don't use the correct syntax.
This probably will work.
create external table test_table(cols varchar(10))
using ( datafiles('DISK:C:\sample.txt'), format 'delimited', deluxe);
About the load
It isn't a native command from the engine, is a specific command of dbaccess utility.
So, if you aren't using dbaccess, it will not work.
BTW, the syntax of your load is correct...

R: run multiple line sql from text file

In R, how can I import the contents of a multiline text file (containing SQL) into multiple lines of SQL?
I've studied Import multiline SQL query to single string and managed to get a simple sql script working. However, when SQL demands a new line (ie when you add a SELECT statement) it doesn't work when you put all lines of sql script in one line.
The sql .txt file looks like:
CREATE TABLE #Countries (Country varchar(255), Region varchar(255))
INSERT INTO #Countries VALUES ('China', 'EM')
SELECT * FROM #Countries
The R code looks like:
fileconn<-file("R/sql.txt","r")
sqlString<-readLines(fileconn)
sqlString<-paste(sqlString,collapse="","")
sqlconn <- odbcDriverConnect(connection = ....)
sqlQuery(sqlconn,sqlString)
I've tried CAT and GSUB as well, but I've got the feeling that the problem occors when the third statement follows the second in one line.
Can anyone help me with this problem? Many thanks.
There are two ways of separating SQL commands. Either you send them separately. This is what you would get by just executing each line of the file in a for loop, but then of course you got the problem what to do if a single command does require more than one line, right? The second way to separate SQL commands is simply to end them with a ;. If you put that at the end of each command, you should be able to pass as many of them as you like to the DB in a single string.