sqlite3.OperationalError: near "--": syntax error - sql
Can't find where is this typo or something - everytime getting this error line 33, in <module>
c_a.execute('ATTACH DATABASE temp.db AS check')
sqlite3.OperationalError: near "check": syntax error
I'm trying to attach database temp.db to the prime.db due one query. This has help me to find duplicate records (checking login (one of the columns in both databases))
import sqlite3
db_a = sqlite3.connect('prime.db')
c_a = db_a.cursor()
db_b = sqlite3.connect('temp.db')
c_b = db_b.cursor()
c_a.execute('ATTACH DATABASE temp.db AS check')
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()
Also tried another version:
attachDatabaseSQL = "ATTACH DATABASE temp.db AS check"
dbSpec = ("temp.db",)
c_a.execute(attachDatabaseSQL, dbSpec)
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()
It found the error:
line 41, in <module>
c_a.execute(attachDatabaseSQL, dbSpec)
sqlite3.OperationalError: near "check": syntax error
Use Python 3.8.1 and SQLite3.
UPDATE:
Changed it to:
c_a.execute('ATTACH DATABASE temp.db AS dbcheck')
c_a.execute('SELECT * FROM check.user_info WHERE user_info.login = user.login')
db_a.commit()
c_a.fetchall()
And the ERROR NOW IS:
c_a.execute('ATTACH DATABASE temp.db AS dbcheck')
sqlite3.OperationalError: no such column: temp.db
ATTACH DATABASE temp.db AS check;
check is a reserved word in SQLite, as in most (if not all) other databases. In the syntax, it comes into play to define check constraints when creating tables.
Consider using another name, that does not conflict with a language keyword. For example:
ATTACH DATABASE temp.db AS dbcheck;
Related
How to resolve an error in Importing CSV file from PostgreSQL
I want to import a CSV file to PostgreSQL. Shown below is the syntax used to import a CSV file to a created table i.e. salary_supervisor in PostgreSQL, COPY salary_supervisor FROM 'X:\XX\XXX\XXX\XXX\XXX\XXX\supervisor_salaries.csv' /* File path */ WITH (FORMAT CSV, HEADER); However, it indicates an error as shown below, ERROR: could not open file "X:\XX\XXX\XXX\XXX\XXX\XXX\supervisor_salaries.csv" for reading: Permission denied HINT: COPY FROM instructs the PostgreSQL server process to read a file. You may want a client-side facility such as psql's \copy. SQL state: 42501 Since an error was shown above, the below syntax was used to import the CSV file to PostgreSQL, \copy (Select * From salary_supervisor) FROM 'X:\XX\XXX\XXX\XXX\XXX\XXX\supervisor_salaries.csv' /* File path */ WITH (FORMAT CSV, HEADER); However, it also indicated a syntax error shown below, ERROR: syntax error at or near "\" LINE 1: \copy (Select * From salary_supervisor) ^ SQL state: 42601 Character: 1
Need to export CSV file from PSQL on Windows (psql 13) using COPY TO
I am using \COPY ( SELECT * FROM person LEFT JOIN car ON car.id=person.car_id ) TO 'c:/Users/nick-/Downloads/results.csv' DELIMITER ',' CSV HEADER; and am getting the error ERROR: syntax error at or near "TO" LINE 6: TO 'c:/Users/nick-/Downloads/results.csv' I've also tried COPY rather than \COPY but I get this error ERROR: could not open file "c:/Users/nick-/Downloads/results.csv" for writing: Permission denied HINT: COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy. I have tried using all of the below c:/Users/nick-/Downloads/results.csv c://Users//nick-//Downloads//results.csv 'c:\Users\nick-\Downloads\results.csv' 'c:\\Users\\nick-\\Downloads\\results.csv' and none have worked.
How can I resolve an Arithmetic Overflow Error when inserting data from a CSV into a SQL Server table?
I am trying to read in a .CSV file of test results into my SQL Server Express database, and I am able to get two of the 25 rows into my database, but then it errors out with the following error: DataError: ('22003', u'[22003] [Microsoft][ODBC SQL Server Driver][SQL Server]Arithmetic overflow error converting varchar to data type numeric. (8115) (SQLExecDirectW); [22003] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)') It doesn't specify which field or column is causing the issue; and I have searched Google and Stack Exchange, but haven't found anything to resolve this issue. Any help would be greatly appreciated. I am using Python 2.7, SQL Server Express, and pyodbc for the connection between the two. Here is the code that I am trying to run: import pyodbc import csv print print("Please wait while I connect to database...") print # Connecting to the Database mydb = pyodbc.connect('Driver={SQL Server};' 'Server=DESKTOP-5I015MM\SQLEXPRESS;' 'Trusted_Connection=yes;') # Turning on autocommit mydb.autocommit = True # Creating a cursor object mycursor = mydb.cursor() mycursor.execute("USE mydatabase") print print("Connection to the database was successful!") # Adding Test Results to database - Look into this... strSQL='''INSERT INTO TestResults(datasource, modelNumber, testSequence, reportingCondition, testDate, isc, voc, imp, vmp, ff, pmp, noct) VALUES ('SolarLabs',?,?,?,?,?,?,?,?,?,?,?)''' #open csv file with open('test_results_NOCT.csv','rb') as f: csvfile= csv.reader(f) next(csvfile) for row in csvfile: mycursor.execute(strSQL, row) print print("Test Results added successfully") print And here is the CSV code that I am trying to read into SQL: Model,Test Sequence,Condition,Date,Isc,Voc,Imp,Vmp,FF,Pmp,NOCT KUT0012,Baseline,STC,3/11/2008,5.2,44.7,4.88,35.7,75,174.3,51.9 KUT0003,Baseline,STC,3/11/2008,5.34,44.7,5.03,35.7,75.2,179.7,52.1 KUT0003,TC200,STC,5/7/2008,5.2,45.1,4.83,36.4,75.2,176.2,- KUT0004,Baseline,STC,3/11/2008,5.21,44.8,4.91,36.1,76,177.2,51.8 KUT0004,TC200,STC,5/7/2008,5.17,45.1,4.81,36.5,75.3,175.6,- KUT0004,Hotspot,STC,6/25/2008,5.09,45.6,4.7,37,74.9,173.7,- KUT0001,Baseline,STC,3/11/2008,5.32,44.6,4.95,35.4,73.8,175.2,51.8 KUT0001,TC200,STC,5/7/2008,5.2,45,4.77,36.8,75.1,175.6,- KUT0006,Baseline,STC,3/11/2008,5.35,44.4,4.95,35.8,74.5,177.2,52 KUT0006,UV,STC,6/5/2008,5.28,44.6,4.84,35.8,73.7,173.7,- KUT0006,TC50,STC,7/4/2008,5.22,45,4.72,36.9,74.1,173.9,- KUT0006,HF10,STC,8/1/2008,5.21,45.1,4.69,37,73.9,173.4,- KUT0006,Termination,STC,8/19/2008,5.23,45,4.62,37.3,73.2,172.5,- KUT0007,Baseline,STC,3/11/2008,5.25,44.4,4.87,35.8,74.6,174.2,52.1 KUT0007,UV,STC,6/5/2008,5.39,43.9,4.84,35.5,72.5,171.7,- KUT0007,TC50,STC,7/4/2008,5.56,44.7,4.87,36.8,72.2,179.3,- KUT0007,HF10,STC,8/1/2008,5.5,44.6,4.85,36.4,72.2,176.8,- KUT0005,Baseline,STC,3/11/2008,5.13,44.3,4.84,35.6,75.7,172.3,51.7 KUT0005,Damp Heat,STC,5/8/2008,5.11,45.5,4.7,37.4,75.4,175.6,- KUT0005,Static Load,STC,5/29/2008,4.95,45.6,4.67,37.6,77.6,175.5,- KUT0008,Baseline,STC,3/11/2008,5.13,44.6,4.84,36,76.2,174.4,52.2 KUT0008,Damp Heat,STC,5/8/2008,5.17,44.9,4.78,36.2,74.4,172.7,- KUT0008,Hail,STC,5/21/2008,5.14,44.5,4.73,35.8,74,169.4,- KUT0011,Baseline,STC,3/11/2008,5.24,44.7,4.99,35.8,76.1,178.4,51.9 KUT0011,Outdoor Exposure,STC,4/17/2008,5.05,44.3,4.79,35.6,76.4,170.7,- And here is the table schema: Table Schema
Error loading yelp dataset to postgreSQL
I'm trying to import the Yelp data from their sql file into a postgreSQL 10 database on Windoes by running the code: psql -U postgres yelp_db < yelp_sql and getting the errors below. As you can see it is probably due to the ` character. Is there any way to easy fix that? The file is 7.3GB so I'd like to avoid having to read it line by line to change the ` character to '. 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`; ^ ERROR: syntax error at or near "`" LINE 1: CREATE TABLE `attribute` (
Importing .sql file into SQLite
I'm trying to import a large .sql file into an SQLite .db file, but I'm getting the following errors: sqlite> .read ./smsCorpus_en_2012.04.30.sql Error: near line 23: near "COMMENT": syntax error Error: near line 50: near "LOCK": syntax error Error: near line 52: near "some1": syntax error Error: near line 58: near "s": syntax error Error: near line 60: near "s": syntax error Error: near line 66: near "UNLOCK": syntax error The file is located at http://wing.comp.nus.edu.sg:8080/SMSCorpus/data/corpus/smsCorpus_en_sql_2012.04.30.zip (direct file link) linked on this page http://wing.comp.nus.edu.sg:8080/SMSCorpus/history.jsp EDIT: just a warning, the file is quite large...not sure if this is the issue?
That file is a MySQL dump. To make SQLite understand it, you have to: delete COMMENTs on the table fields; remove AUTO_INCREMENT from id (INTEGER PRIMARY KEY fields are autoincrementing in SQLite anyway); remove ENGINE and DEFAULT CHARSET; remove LOCK/UNLOCK commands; make the INSERT commands have fewer records; replace \' quoting with ''.
That is a MySQL Dump and not SQLite. There are slight variations on the syntax.