The following code causes a syntax error in SQLite.
I imagine this must be simple but I can't see it.
I have two tables, pages and items, linked through a text code. When this code in changed in pages table, I want it changed in items table too.
The SQLite version is quite old, 3.7.7.1, but I haven't seen such a problem in version history. This occurs on Windows, with PHP 5.3.1.
CREATE TABLE IF NOT EXISTS "efpage" ( "pageid" integer NOT NULL PRIMARY KEY, "psite" text NULL, "pgcode" text NULL );
CREATE TABLE IF NOT EXISTS "efitem" ( "itemid" integer NULL PRIMARY KEY , "isite" text NULL, "ititle" text NULL, "inpage" text NULL);
DROP TRIGGER IF EXISTS update_pagecode;
CREATE TRIGGER update_pagecode AFTER UPDATE OF pgcode ON efpage
BEGIN
UPDATE efitem SET inpage = new.pgcode WHERE inpage = old.pgcode;
END;
The resulting error is
ERROR near "pgcode": syntax error.
Trying different things, I saw that the syntax error is after "old.pgcode;"
As indicated by CL,
this is linked to the version of SQLite.
I use SQLite bundled with PHP on windows.
The next (answered) question for me is :
How to update the SQLite version bundled with PHP
Related
Hi everyone I am having some problems with my SQLite database in my java program. I am trying to retrieve data from a couple of tables but it says my table doesn't exist. I have checked using DB Browser and it's definitely there, so I'm not sure what I'm doing wrong. This is the error I receive:
[SQLITE_ERROR] SQL error or missing database (no such table: staff_clocked_in.clock_in_time)
SELECT * FROM staff, staff_clocked_in.clock_in_time WHERE staff.staff_id = staff_clocked_in.staff_id;
I'm sure my tables exist and there is data in both tables, here is a screenshot of my db browser.
If it helps, this is how I have setup my tables:
STAFF:
CREATE TABLE IF NOT EXISTS staff (staff_id INTEGER PRIMARY KEY NOT NULL, first_name TEXT NOT NULL, last_name TEXT NOT NULL, job_title TEXT NOT NULL);
STAFF_CLOCKED_IN:
CREATE TABLE IF NOT EXISTS staff_clocked_in (staff_id INTEGER PRIMARY KEY NOT NULL REFERENCES staff(staff_id), clock_in_time DATETIME NOT NULL);
Can anyone see anything wrong with my query? I'm not good with databases so hopefully it's just something simple.
The error message is correct staff_clocked_in.clock_in_time is not a table.
You should use staff_clocked_in instead which is your table.
So the fixed query should look like
SELECT *
FROM staff, staff_clocked_in
WHERE staff.staff_id = staff_clocked_in.staff_id;
Im trying to alter my table "Materiel" for make a primary key.,but this instruction is not working.
ALTER TABLE "MATERIEL" ADD CONSTRAINT "MATERIEL_PK" PRIMARY KEY ("MATRICULE")
This is stack trace:
[00:22:39] Error while executing SQL query on database 'Data': near
"CONSTRAINT": syntax error
Of course it won't work - that syntax is not supported by SQLite3 (see https://sqlite.org/lang_altertable.html). To do this, you need to re-create the table to include the constraint.
Whenever I try to delete ANY field from ANY content type I get the following error:
Uncaught PHP Exception Drupal\\Core\\Database\\DatabaseExceptionWrapper: "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'drupal.field_deleted_data_35ab99eaa1' doesn't exist: SELECT DISTINCT t.entity_id AS entity_id\nFROM \n{field_deleted_data_35ab99eaa1} t\nWHERE bundle = :db_condition_placeholder_0\nLIMIT 10 OFFSET 0; Array\n(\n [:db_condition_placeholder_0] => slider_images\n)\n" at /var/www/html/web/core/lib/Drupal/Core/Database/Connection.php line 685
The only difference being the table hash data, I.E. deleted_data_xxxx, each field i try to delete is referencing a different table. I've tried reinstalling Drupal and reimporting my configuration but no luck.
Any suggestions?
UPDATE:
After checking the database there are many of these tables:
field_deleted_revision_df347fb61b
and
field_deleted_df347fb61b
If that makes any difference.
I experienced this also, dig down the code. I found this all reason why fields not deleted after you did, the following:
executing cron gazillion times
The entries in field_config and field_config_instance have probably a value of 1 in the deleted column.
This means they're marked for deletion, but won't actually be deleted until you run cron (deleted field data is purged in field_cron()).
As an alternative to running cron to remove deleted data, you can manually run field_purge_batch($batch_size).
The $batch_size to use will vary depending on your server environment and needs. I've used values as low a 5 and as high as 10000.
Here more informations about the field_purge_batch() function.
Here a possible solution to resolve your issue, but backup your database first. Don't be lazy, it will save your ass, if something goes wrong.
using drush:
drush eval "field_purge_batch(500)"
you might have to run a few times, or increase the $batch_size then there might still be field_deleted and field_deleted_revision tables, even after running cron
using SQL query:
SELECT * FROM `field_config` WHERE `deleted` = 1
SELECT * FROM `field_config_instance` WHERE `deleted` = 1
if you come up empty, you can safely delete those leftover tables.
The only way I could resolve this was to create the missing table from the sql command line (using drush sql-cli), for example:
CREATE TABLE `field_deleted_data_XXX` (
`bundle` varchar(128) CHARACTER SET ascii NOT NULL DEFAULT '',
`deleted` tinyint(4) NOT NULL DEFAULT '0',
`entity_id` int(10) unsigned NOT NULL,
`revision_id` int(10) unsigned NOT NULL,
`langcode` varchar(32) CHARACTER SET ascii NOT NULL DEFAULT '',
`delta` int(10) unsigned NOT NULL,
`content_translation_source_value` varchar(12) CHARACTER SET ascii NOT NULL,
PRIMARY KEY (`entity_id`,`deleted`,`delta`,`langcode`),
KEY `bundle` (`bundle`),
KEY `revision_id` (`revision_id`) );
Replacing XXX with the code that follows field_deleted_data_ in the error message. Then run:
drush php-eval 'field_purge_batch(1000);'
This may generate the error with a new code. I had to go through the process 3 times, but eventually it resolved the error.
I am trying to use a SQLite plugin for PhoneGap for Windows Phone 8. I've found 2 or 3 different plugins (maybe all of them are based from the same base) and all give me an error when the applicattion is executed.
The plugin I'm using is from here: https://github.com/marcucio/Cordova-WP-SqlitePlugin
I am including also the dll. I'm using PhoneGap 3.3.
When I execute the application, the plugin is loaded (it seems is loades correctly) and some operations are done correctly (some CREATE and some INSERT are performed) but at a given moment the Visual Studio gives me this error infinite times:
An exception of type 'System.Security.SecurityException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.Security.SecurityException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll
I've tried other similar plugins and the error still appears.
The other threads in stackoverflow related to this problem didn't solve the problem. The application always crashes in an IsolatedStorage operation.
Anyone had the same problem and could solve it? If I don't perform any INSERT operation the plugin doesn't crash.
Thank you.
I had the same problem using Community.CsharpSqlite.WinPhone, it's possible that we have the same issue.
This happened when I did an INSERT OR REPLACE or INSERT OR IGNORE with a table which primary key was TEXT and had NOT NULL constraint. If the primary key is INTEGER and AUTOINCREMENT, it works fine.
For example:
CREATE TABLE table1 (
tableid TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY(tableid)
);
INSERT or REPLACE INTO table1 (tableid, value) VALUES ("id1","test");
If your id column has NOT NULL constraint, SQLite will return a System.Security.SecurityException. Try to remove this constraint:
CREATE TABLE table1 (
tableid TEXT,
value TEXT NOT NULL,
PRIMARY KEY(tableid)
);
It's a bug, in other platforms (Android, iOS, etc) the table can have a NOT NULL constraint and it works fine.
In MySQL Workbench you can add COMMENTs to tables and columns in a MySQL database.
Does Sqlite support adding comments to tables and columns?
I don't think it does. The "SQL As Understood By SQLite" page makes no mention of table or column comments nor does the CREATE TABLE or ALTER TABLE documentation.
Also, the Unsupported SQL wiki page has this:
2009-08-04: Table and column comments - I have scoured the doco and can't find anything about applying comments to tables or their columns.
Yes, that's a wiki page from 2009 but that note is supported by the rest of the documentation.
However, SQLite does preserve SQL comments that you put in your DDL. If you feed this to the sqlite3 CLI tool:
CREATE TABLE User
-- A table comment
(
uid INTEGER, -- A field comment
flags INTEGER -- Another field comment
);
Then you get exactly that back from a .schema command:
sqlite> .schema
CREATE TABLE User
-- A table comment
(
uid INTEGER, -- A field comment
flags INTEGER -- Another field comment
);
So you should be able to fake it if you can control the DDL used to create your tables.
When creating a table using sqlite (I'm using sqlite3 in python), the COMMENT section is not supported.
This fails (works in full MySql syntax):
CREATE TABLE `Info` (
`Test` VARCHAR(512) NOT NULL COMMENT 'Column info here'
);
This works (no COMMENT in the column declaration):
CREATE TABLE `Info` (
`Test` VARCHAR(512) NOT NULL
);
(This isn't what the original poster was asking, but this is what I was looking for when I first found this question based on the keywords in the title.)
How to make comments in SQLite
There are two ways to make comments in SQLite code:
Hyphens
-- this is my comment
SELECT * FROM employees;
C-style
/* this is my comment */
SELECT * FROM employees;
I appreciate that this is an old post but for what it's worth, you can add comments when creating a table in SQLITE3, in Python and in Java. Probably works for other languages as well.
You need to add new lines to your sql string as you would if you were typing in the command at the SQLITE3 prompt -
sql_str = 'CREATE TABLE properties (\nproperty TEXT NOT NULL, -- A property\nvalue TEXT -- The value of the property\n);'
When executed the table is created like so:
sqlite> .schema
CREATE TABLE properties (
property TEXT NOT NULL, -- A property
value TEXT -- The value of the property
);
I suspect that this works because the connector is actually echoing in the commands via the command prompt, rather than some sort of API.