Error ")" near first line when building SQLite database - database

I am trying to a build an SQLite database from an existing CSV file following the steps from this answer. However I already get an error in the second step where the database should be created from the defined table with
sqlite3 worldcities.db < worldcities.sql
The error message reads
Error: near line 1: near ")": syntax error
My worldcities.sql file looks like this:
CREATE TABLE worldcities(
city TEXT NOT NULL,
city_ascii TEXT NOT NULL,
lat REAL NOT NULL,
lng REAL NOT NULL,
country TEXT NOT NULL,
iso2 TEXT NOT NULL,
iso3 TEXT NOT NULL,
admin_name TEXT NOT NULL,
capital TEXT NOT NULL,
population INT NOT NULL,
id INT NOT NULL,
);
What did I do wrong here?

I suspect the orphaned , just before your )to cause your problem.
I tried here https://extendsclass.com/sqlite-browser.html
this
CREATE TABLE newtoy2 (MID INT, BID INT);
without a problem.
But this, having the same problem I suspect in your code
CREATE TABLE newtoy1 (MID INT, BID INT,);
gets the same error you quote: near ")": syntax error
Which confirms my suspicion.

There was an extra comma ',' before end of block.
Commas are used after all column description but not after the last column description.
Try and Run this below code.
create table worldcities(
city TEXT NOT NULL,
city_ascii TEXT NOT NULL,
lat REAL NOT NULL,
lng REAL NOT NULL,
country TEXT NOT NULL,
iso2 TEXT NOT NULL,
iso3 TEXT NOT NULL,
admin_name TEXT NOT NULL,
capital TEXT NOT NULL,
population INT NOT NULL,
id INT NOT NULL);

Related

Oracle Spatial search query error for SDO_WITHIN_DISTANCE (lat/long based)

I need to perform location based search based on latitude and longitude pairs, to find out the near by locations of stores within a radius/distance of given KMs.
I am to use (for certain) Oracle's SDO_GEOMETRY for searching the locations.
Also, the table structure are parent-child based such that store address (zip, lat/long) is in parent table but store details (name, contact, etc) are in the child table.
This is to make sure that we do not have redundant data (as multiple store can have same lat/long and by some dark magic same address)
I have the following scenario (tables provided):
Version - Oracle Database 12c Enterprise Edition Release 12.2.0.1.0
Table: STORE_LOCATION
CREATE TABLE STORE_LOCATE
(
ID NUMBER DEFAULT STORE_LOCATE.nextval,
POSTAL_CODE VARCHAR2(18) NOT NULL,
ADDRESS VARCHAR2(382) NOT NULL,
GEO_LOCATION SDO_GEOMETRY NOT NULL
);
Table: STORE_DETAIL
CREATE TABLE STORE_DETAIL
(
ID NUMBER DEFAULT STORE_DETAIL_SEQ.nextval,
STORE_CODE VARCHAR2(20) NOT NULL,
STORE_NAME VARCHAR2(150) NOT NULL,
IS_ACTIVE NUMBER(3) DEFAULT 0 NOT NULL,
fk_store_locate_id INT NOT NULL. -- FK to ID of parent
);
I have inserted data in parent as:
INSERT INTO STORE_LOCATE (ZIP, ADDRESS, GEO_LOCATION) VALUES
('567875', '84 Paddar Road',
SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (80.34234431,26.12354422, NULL), NULL, NULL));
Likewise, child table has entries:
INSERT INTO STORE_DETAIL (STORE_CODE, STORE_NAME, FK_STORE_LOCATION_ID) VALUES
('ST01', 'STORE 1', 1);
So, now when a user shares his location to me (lat/long), I need to search all nearby locations of stores within a specified radius.
I tried the following query, but I am getting error with this:
SELECT
s.store_code,
s.store_name,
loc.zip,
loc.address,
loc.geo_location
FROM store_detail s,
store_locate loc
where MDSYS.SDO_WITHIN_DISTANCE(loc.geo_location,
(MDSYS.SDO_GEOMETRY(2001, 8307,
MDSYS.SDO_POINT_TYPE(80.21456732,26.23117864, NULL) ,NULL, NULL)),
'distance=1000 unit=KM') = 'TRUE';
Getting the below error:
ORA-29900: operator binding does not exist
ORA-06553: PLS-306: wrong number or types of arguments in call to 'SDO_WITHIN_DISTANCE'
29900. 00000 - "operator binding does not exist"
*Cause: There is no binding for the current usage of the operator.
*Action: Change the operator arguments to match any of the existing
bindings or add a new binding to the operator.
I am breaking my head around this for a while now but in vain.
Ref:
https://issues.redhat.com/browse/TEIID-751?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel
https://docs.oracle.com/cd/E17781_01/appdev.112/e18750/xe_locator.htm#XELOC562
Q's:
Is this the correct way to perform location search on Oracle SQL?
Is there any other way if the above is incorrect?
There are a number of syntax errors in your code (POSTAL_CODE called ZIP, etc)
Here is the corrected example (I also removed the sequences for simplicity)
CREATE TABLE STORE_LOCATE (
ID NUMBER primary key,
POSTAL_CODE VARCHAR2(18) NOT NULL,
ADDRESS VARCHAR2(382) NOT NULL,
GEO_LOCATION SDO_GEOMETRY NOT NULL
);
CREATE TABLE STORE_DETAIL (
ID NUMBER primary key,
STORE_CODE VARCHAR2(20) NOT NULL,
STORE_NAME VARCHAR2(150) NOT NULL,
IS_ACTIVE NUMBER(3) DEFAULT 0 NOT NULL,
fk_store_locate_id INT NOT NULL references STORE_LOCATE
);
INSERT INTO STORE_LOCATE (ID, POSTAL_CODE, ADDRESS, GEO_LOCATION) VALUES (1, '567875', '84 Paddar Road', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (80.34234431,26.12354422, NULL), NULL, NULL));
INSERT INTO STORE_DETAIL (ID, STORE_CODE, STORE_NAME, FK_STORE_LOCATE_ID) VALUES (1001,'ST01', 'STORE 1', 1);
commit;
Here is running your query:
SELECT
s.store_code,
s.store_name,
loc.postal_code,
loc.address,
loc.geo_location
FROM store_detail s, store_locate loc
where SDO_WITHIN_DISTANCE(
loc.geo_location,
SDO_GEOMETRY(2001, 8307,
SDO_POINT_TYPE(80.21456732,26.23117864, NULL),NULL, NULL
),
'distance=1000 unit=KM'
) = 'TRUE';
STORE_ STORE_NAME POSTAL ADDRESS GEO_LOCATION(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES
------ ---------- ------ -------------------- ----------------------------------------------------------------------------------
ST01 STORE 1 567875 84 Paddar Road SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE(80.3423443, 26.1235442, NULL), NULL, NULL)
1 row selected.
The error you get probably means Oracle Spatial is not or incorrectly installed in your database. What exact database version do you use ?
IMPORTANT NOTE:
Those queries really need a spatial index on the GEO_LOCATION column. Without one, the query is rejected in versions up to 12.1. From 12.2 and later, the query will run, but will be very slow as soon as the number of locations goes above a few 100s. Once you go into the 100000 or more, it will be very slow.
See https://docs.oracle.com/en/database/oracle/oracle-database/19/spatl/indexing-querying-spatial-data.html#GUID-07129836-0DAE-4BCC-B290-942C456AE2EA for details

Some SQLite tables can not be created, but others (using the same approach) can

I am making an Electron application, which uses an SQLite Database. Inside of the app's directory, I have a folder named db and as a child, another one - seeder. In that seeder directory there are a bunch of .sql files, needed for creating the db architecture and seeding the database with information needed:
Electron App/
|---main.js
|---index.html
|---node_modules
|---package.json
|---db/
|---seed.js
|---seeder/
|---bunch of .sql files
When I run seed.js it is supposed to get all filenames inside of seeder/ and run every single one successively. The files are named as follows:
01.tables.sql
02.countries.sql
03.ekatte.sql - (this one contains all the villages and cities in Bulgaria)
04.programmes.sql
05.schedules.sql
06.subjects.sql
Everything works as expected, except one thing: 01.tables.sql is supposed to create all the tables, and all other files contain INSERT INTO statements. Some of the tables are not created and when the Electron app tries to INSERT INTO them - sqlite returns an error: no such table (but only for oblasti, programmes, schedules and subjects).
Here are the contents of 01.table.sql:
CREATE TABLE countries (
countryID integer NOT NULL CONSTRAINT countries_pk PRIMARY KEY,
countryName text NOT NULL,
countryISO text NOT NULL
);
CREATE TABLE grades (
gradeID integer NOT NULL CONSTRAINT grades_pk PRIMARY KEY,
gradeValue integer NOT NULL,
gradeStudent integer NOT NULL,
gradeSchedule integer NOT NULL
);
CREATE TABLE oblasti (
oblastID integer NOT NULL CONSTRAINT oblasti_pk PRIMARY KEY,
oblastName text NOT NULL,
oblastCode text NOT NULL
);
CREATE TABLE obshtini (
obshtinaID integer NOT NULL CONSTRAINT obshtini_pk PRIMARY KEY,
obshtinaName text NOT NULL,
obshtinaOblast integer NOT NULL
);
CREATE TABLE programmes (
programmeID integer NOT NULL CONSTRAINT programmes_pk PRIMARY KEY,
programmeName text NOT NULL
);
CREATE TABLE schedules (
scheduleID integer NOT NULL CONSTRAINT schedules_pk PRIMARY KEY,
scheduleProgramme integer NOT NULL,
scheduleSemester integer NOT NULL,
scheduleSubject integer NOT NULL
);
CREATE TABLE selishta (
selishteID integer NOT NULL CONSTRAINT selishta_pk PRIMARY KEY,
selishteName text NOT NULL,
selishteObshtina integer NOT NULL,
selishteOblast integer NOT NULL
);
CREATE TABLE students (
studentID integer NOT NULL CONSTRAINT students_pk PRIMARY KEY,
studentName text NOT NULL,
studentFamily text NOT NULL,
studentPicture blob NOT NULL,
studentBirthplace integer,
studentForeignCountry integer,
studentForeignOblast text,
studentForeignObshtina text,
studentForeignSelishte text,
studentEGN integer NOT NULL,
studentAddress text NOT NULL,
studentPhone text NOT NULL,
studentNationality integer NOT NULL,
studentEduForm integer NOT NULL,
studentOKS integer NOT NULL,
studentSemester integer NOT NULL,
studentClass integer NOT NULL,
studentFakNomer integer NOT NULL,
studentStatus integer NOT NULL,
studentValid integer NOT NULL
);
CREATE TABLE subjects (
subjectID integer NOT NULL CONSTRAINT subjects_pk PRIMARY KEY,
subjectName text NOT NULL
);
and this is the part of seed.js, responsible for the actual seeding:
const db = new sqlite3.Database('db/database.db');
fs.readdir('db/seeder/', function(err, dir) {
let fileList = new Array();
$.each(dir, function(index, fileName){
fileList.push(fileName);
});
fileList.sort();
seedDB(fileList);
});
function seedDB(files){
if(files.length > 0){
let fileName = files.shift();
fs.readFile('db/seeder/'+fileName, 'utf-8', function(err, data){
db.serialize(function() {
db.run(data);
});
seedDB(files);
});
}
}

Extracted data using DDL query saved but FORMAT has chaged

I have EXTRATED data using
spool abc.sql
select DBMS_METADATA.GET_DDL('${o_type}', '${o_name}', 'WEBADM1') from dual;
spool off
And saved in a file with name abc.
Now the data which has been saved is in diffrent formate,The formate has been changed,
i finding new line in the data
eg
WHEN 'P' THEN
X.AG_PROD_MOD_NR
ELSE
V_
ITEM_NO
END,
V_TOP_MODEL_NBR,
V_
SRT_VALUE,
V_PROD_DESC,
V_PROD
UCT_TYPE,
NULL,
NULL,
NULL,
V_CONF
IG_HEADER_ID,

Two queries on Sybase giving different result

Below are two proc running in sybase. The first has param name with value, the second only value. The first one runs fine, but when I run the second I get Implicit conversion from datatype 'INT' to 'VARCHAR' is not allowed. Use the CONVERT function to run this query Can someone tell me why?
First:
exec pu #a=null, #b=null, #c=null, #d=null, #e=null, #f=null, #g='2013-Jun-12 22:10:00.670', #h=100, #i=2, #j=null, #k=null, #l=null, #m=null, #n=0, #o='P', #p=null, #q=null, #r=null, #s=null, #t='junit', #u=null, #v=null, #w=null
Second:
exec pu ( null, null, null, null, null, null, '2013-Jun-12 22:10:00.187', 100, 2, null, null, null, null, 0, 'P', null, null, null, null, 'junit', null, null, null )
What could also be happening is that the parameters are not defined in the same as order as you are expecting them.
In scenario 1, you won't see a problem since parameters are referenced by name.
In scenario 2 however you are using positional reference which could be exposing this issue.
Please check the proc definition and make sure parameters are declared in same order as expected.

How to convert an anorm resultlist from a two-field data table to a Tuple2, in Scala

I have the following table (maximum number of records 999) I use for a lookup:
CREATE TABLE lga
(
lgacode character varying(3) NOT NULL DEFAULT '000'::character varying,
lganame character varying(32) NOT NULL,
CONSTRAINT pk_lga PRIMARY KEY (lganame),
CONSTRAINT uk_lga UNIQUE (lgacode)
);
Using Anorm, I easily get a result list lgas of the type List[models.LgaTable]
How do I get this result list into the form List[Tuple2[String,String]]?
I searched Stack Overflow and found something close ([a link]http://stackoverflow.com/questions/4927260/filling-a-scala-immutable-map-from-a-database-table) but this contained Set, which I have an aversion for, meanwhile: I just needed something simple. Thanks
Just call a map on your result:
val lgas: List[models.LgaTable] = ...
val lgas_tupled = lags.map(row => (row.lgacode, lganame))

Resources