Using Limit and offset in Sqlite update statmet - sqlite

update table set column_name limit 3 offset 2;
The above query is not working.
Throws error
sql error: syntax error near 'limit'.

An UPDATE statement expects a new value after the column_name, like this:
update thetable set column_name = 'some new value'
Furthermore, the documentation mentions that you need to have compiled SQLite with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT option, which is not enabled by default.

Sqlite does not allow the use of LIMIT and OFFSET statements like in MYSQL. You will have to use a nested query to workaround it . Or use two queries.

Related

Heroku Postgres thowing an errror-> message relation "aspnetusers" does not exist from AspNetUsers [duplicate]

I'm trying to run the following PHP script to do a simple database query:
$db_host = "localhost";
$db_name = "showfinder";
$username = "user";
$password = "password";
$dbconn = pg_connect("host=$db_host dbname=$db_name user=$username password=$password")
or die('Could not connect: ' . pg_last_error());
$query = 'SELECT * FROM sf_bands LIMIT 10';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
This produces the following error:
Query failed: ERROR: relation "sf_bands" does not exist
In all the examples I can find where someone gets an error stating the relation does not exist, it's because they use uppercase letters in their table name. My table name does not have uppercase letters. Is there a way to query my table without including the database name, i.e. showfinder.sf_bands?
From what I've read, this error means that you're not referencing the table name correctly. One common reason is that the table is defined with a mixed-case spelling, and you're trying to query it with all lower-case.
In other words, the following fails:
CREATE TABLE "SF_Bands" ( ... );
SELECT * FROM sf_bands; -- ERROR!
Use double-quotes to delimit identifiers so you can use the specific mixed-case spelling as the table is defined.
SELECT * FROM "SF_Bands";
Re your comment, you can add a schema to the "search_path" so that when you reference a table name without qualifying its schema, the query will match that table name by checked each schema in order. Just like PATH in the shell or include_path in PHP, etc. You can check your current schema search path:
SHOW search_path
"$user",public
You can change your schema search path:
SET search_path TO showfinder,public;
See also http://www.postgresql.org/docs/8.3/static/ddl-schemas.html
I had problems with this and this is the story (sad but true) :
If your table name is all lower case like : accounts
you can use: select * from AcCounTs and it will work fine
If your table name is all lower case like : accounts
The following will fail:
select * from "AcCounTs"
If your table name is mixed case like : Accounts
The following will fail:
select * from accounts
If your table name is mixed case like : Accounts
The following will work OK:
select * from "Accounts"
I dont like remembering useless stuff like this but you have to ;)
Postgres process query different from other RDMS. Put schema name in double quote before your table name like this, "SCHEMA_NAME"."SF_Bands"
Put the dbname parameter in your connection string. It works for me while everything else failed.
Also when doing the select, specify the your_schema.your_table like this:
select * from my_schema.your_table
If a table name contains underscores or upper case, you need to surround it in double-quotes.
SELECT * from "Table_Name";
I had a similar problem on OSX but tried to play around with double and single quotes. For your case, you could try something like this
$query = 'SELECT * FROM "sf_bands"'; // NOTE: double quotes on "sf_Bands"
This is realy helpfull
SET search_path TO schema,public;
I digged this issues more, and found out about how to set this "search_path" by defoult for a new user in current database.
Open DataBase Properties then open Sheet "Variables"
and simply add this variable for your user with actual value.
So now your user will get this schema_name by defoult and you could use tableName without schemaName.
You must write schema name and table name in qutotation mark. As below:
select * from "schemaName"."tableName";
I had the same issue as above and I am using PostgreSQL 10.5.
I tried everything as above but nothing seems to be working.
Then I closed the pgadmin and opened a session for the PSQL terminal.
Logged into the PSQL and connected to the database and schema respectively :
\c <DATABASE_NAME>;
set search_path to <SCHEMA_NAME>;
Then, restarted the pgadmin console and then I was able to work without issue in the query-tool of the pagadmin.
For me the problem was, that I had used a query to that particular table while Django was initialized. Of course it will then throw an error, because those tables did not exist. In my case, it was a get_or_create method within a admin.py file, that was executed whenever the software ran any kind of operation (in this case the migration). Hope that helps someone.
In addition to Bill Karwin's answer =>
Yes, you should surround the table name with double quotes. However, be aware that most probably php will not allow you to just write simply:
$query = "SELECT * FROM "SF_Bands"";
Instead, you should use single quotes while surrounding the query as sav said.
$query = 'SELECT * FROM "SF_Bands"';
You have to add the schema first e.g.
SELECT * FROM place.user_place;
If you don't want to add that in all queries then try this:
SET search_path TO place;
Now it will works:
SELECT * FROM user_place;
Easiest workaround is Just change the table name and all column names to lowercase and your issue will be resolved.
For example:
Change Table_Name to table_name and
Change ColumnName to columnname
It might be silly for a few, but in my case - once I created the table I could able to query the table on the same session, but if I relogin with new session table does not exits.
Then I used commit just after creating the table and now I could able to find and query the table in the new session as well. Like this:
select * from my_schema.my_tbl;
Hope this would help a few.
Make sure that Table name doesn't contain any trailing whitespaces
Try this: SCHEMA_NAME.TABLE_NAME
I'd suggest checking if you run the migrations or if the table exists in the database.
I tried every good answer ( upvote > 10) but not works.
I met this problem in pgAdmin4.
so my solution is quite simple:
find the target table / scheme.
mouse right click, and click: query-tool
in this new query tool window, you can run your SQL without specifying set search_path to <SCHEMA_NAME>;
you can see the result:

Can I use the 'WHERE' clause in an 'INSERT' command in SQLite3?

I am using SQLite3 with Python. I am quite certain that the 'WHERE' clause does not work with 'INSERT' operation, but I really need to have a workaround to solve my issue. I have the following prepopulated database:
I was hoping to come up with an SQL statement where I can add the VALUES ('2021-01-13', '36.8') to the table WHERE family='FAA' AND model='MAA'. I have read a lot of stuff online but still no luck on my side.
I think you want an update here:
UPDATE yourTable
SET date = '2021-01-13', duration = 36.8
WHERE family = 'FAA' AND model = 'MAA';
You want to update your table, insert is only for new rows. When you want to change a value, you must to use update statement.
UPDATE table_name
SET date = '2021-01-13', duration = '36.8'
WHERE family='FAA' AND model='MAA';
https://www.sqlite.org/lang_update.html

Sqlite: conditionally query two tables, one will not exist

I am sure I can solve this programmatically, but I am curious if I can do this in one query.
Context:
I will be querying multiple databases, some will have a table; 'table', others will instead have the table; 'table_v2'. I want to run the same SELECT statement on the table that exists. I know I can check if a table exists, but I want to know if I can do it all in one statement.
psuedo code summary of what I want to do in one statement:
if 'SELECT name FROM sqlite_master WHERE type='table' AND name=''table'; is not empty:
SELECT * FROM table;
else
SELECT * FROM table_v2;
I am beholden to constraints out of my control.
Thoughts:
Could I have the table name be a regex?
Can I run both SELECTS, ignore the failed result, and just return the success?
Generally, you can't do either. The query planner in SQLite needs to know the name of the table beforehand, and it must be valid so it can determine which paths to take.
You can use the loadable extension eval to build up the SQL query based off of the schema. Though, this exposes a variant of the same issue, since the query planner needs the table name, you need to build up the entire SQL statement, then run it, so you'll need two eval calls.
SELECT EVAL(
'SELECT * FROM ''' ||
EVAL('SELECT name FROM sqlite_master WHERE type=''table'' AND name IN (''table'', ''table_v2'');') ||
''';'
);
To use the eval function, you'll need to either build and load the extension as a library, or build it into your own custom build of SQLite itself.
Of course, I can't answer if you should do this.

Delphi FireDAC error when loading SQLite3.dll

I already downloaded the latest SQLite.dll from SQLite Download Page and try to load it using TFDPhysDriverLink.VendorLib
But when I run the app, which contains the following code:
procedure TForm1.FormCreate(Sender: TObject);
begin
FDConnection1.Close;
FDPhysSQLiteDriverLink1.Release;
FDPhysSQLiteDriverLink1.VendorLib:= 'Path\SQLite3.dll';
FDQuery1.Open('SELECT *, ROW_NUMBER() OVER() Col FROM TableName');
end;
It throws:
[FireDAC][Phys][SQLite] ERROR: near "(": syntax error
Which means that the window function ROW_NUMBER() is not recognized.
What I'm doing wrong?
How can I force FireDAC to use the latest SQLite.dll?
SQLite do not support ROW_NUMBER.
Look at the answers for this question, you'll probably find something to replace ROW_NUMBER.
If you get this error, then the SQlite3.dll was loaded just fine.
Just use the RowID field, which is always existing for any standard SQLite3 table - unless you explicitly created them with CREATE TABLE WITHOUT ROWID statement.
So I would just write:
FDQuery1.Open('SELECT *, RowID FROM TableName');
Note that if there is an explicit INTEGER PRIMARY KEY column in your table, it will in fact map the internal RowID column. Check the SQLite3 documentation for how this works.

Set Datefirst in read.jdbc SparkR

I'm querying an Azure-SQL-database from Databricks using SparkR's read.jdbc function.
I have no issues sending queries to the database, but, I would like to set the beginning of the week on Sunday using SET DATEFIRST 7; and I cannot find a way to do so.
I have read the answers to this question and I could use some of them as a workaround. However, I would like to know if there is a way to set the DATEFIRST from Databricks (I don't mind using python or Scala to do it).
My piece of code looks like:
query <- "SET DATEFIRST 7;
(SELECT
DATEPART(yyyy,[calday]) * 100 + DATEPART(WEEK,[calday]) as calyearweek
FROM [dbo].[table]) out"
table <-
read.jdbc(
url = jdbcUrl,
database = jdbcDatabase ,
tableName = query,
user = user,
password = password
)
If I erase SET DATEFIRST 7; I get the query results.
If I keep it I receive the following error:
Error in jdbc : com.microsoft.sqlserver.jdbc.SQLServerException:
Incorrect syntax near the keyword 'SET'
7 is the default in SQL Azure as you can confirm with ##DATEFIRST function. You don't have to set DATEFIRST to 7.
SELECT ##DATEFIRST;
If you need set different values for DATEFIRST try to set it from inside a stored procedure.

Resources