SQLite: how to SELECT a range of records? - c++

I want to create a DB table and a set of commands for it. So currently I have
this->command_create_files_table = "CREATE TABLE IF NOT EXISTS files (encoded_url varchar(300) UNIQUE NOT NULL primary key, file_name varchar(150) NOT NULL, user_name varchar(65) NOT NULL, is_public BOOLEAN NOT NULL, modified DATETIME NOT NULL default CURRENT_TIMESTAMP )";
this->command_create_file = "INSERT INTO files (encoded_url, file_name, user_name, is_public ) VALUES (:encoded_url, :file_name, :user_name, :is_public)";
this->command_update_file = " UPDATE files SET encoded_url=:new_encoded_url, file_name=:new_file_name, is_public=:is_public, modified=CURRENT_TIMESTAMP WHERE encoded_url=:encoded_url";
this->command_delete_file = "DELETE FROM files WHERE encoded_url=:encoded_url";
this->command_find_file = "SELECT file_name, user_name, is_public, modified FROM files WHERE encoded_url=:encoded_url";
this->command_find_all_user_files = "SELECT encoded_url, file_name, user_name, is_public, modified FROM files WHERE user_name=:user_name";
(using sqlite3pp syntax here)
I wonder how to SELECT some predefined :N (25 for example - no more) files or less from some start DATETIME point (CURRENT_TIMESTAMP for example)?

SELECT
fields_you_want_to_select
FROM
filesTbl
WHERE
DATETIME > start_of_range AND
DATETIME < end_of_range
LIMIT limit_num

SELECT
fields_you_want_to_select
FROM
filesTbl
WHERE
DATETIME > start_of_range AND
DATETIME < end_of_range
LIMIT
25

Related

GCP BigQuery how to set expiration date to table by python api

I am using BigQuery Python API to create table, and would like to set an expiration date to the table, so the table would be automatically dropped after certain days.
Here is my code:
client = bq.Client()
job_config = bq.QueryJobConfig()
dataset_id = dataset
table_ref = client.dataset(dataset_id).table(filename)
job_config.destination = table_ref
job_config.write_disposition = 'WRITE_TRUNCATE'
dt = datetime.now() + timedelta(seconds=259200)
unixtime = (dt - datetime(1970,1,1)).total_seconds()
expiration_time = unixtime
job_config.expires = expiration_time
query_job = client.query(query, job_config=job_config)
query_job.result()
The problem is that the expiration parameter doesn't seem to work. When I am checking the table detail in the UI, the expiration date is still Never.
To answer a slightly different question, instead of specifying the expiration as part of the request options, you can use a CREATE TABLE statement instead, where the relevant option is expiration_timestamp. For example:
CREATE OR REPLACE TABLE my_dataset.MyTable
(
x INT64,
y FLOAT64
)
OPTIONS (
expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 3 DAY)
);
This creates a table with two columns that will expire three days from now. CREATE TABLE supports an optional AS SELECT clause, too, if you want to create the table from the result of a query (the documentation goes into more detail).
To update an existing table expiration time with Python:
import datetime
from google.cloud import bigquery
client = bigquery.Client()
table = client.get_table("project.dataset.table")
table.expires = datetime.datetime.now() + datetime.timedelta(days=1)
client.update_table(table, ['expires'])
Credits: /u/ApproximateIdentity
Looking at the docs for the query method we can see that it's not possible to set an expiration time in the query job config.
The proper way of doing so is setting at the Table resource, something like:
client = bq.Client()
job_config = bq.QueryJobConfig()
dataset_id = dataset
table_ref = client.dataset(dataset_id).table(filename)
table = bq.Table(table_ref)
dt = datetime.now() + timedelta(seconds=259200)
table.expires = dt
client.create_table(table)
query_job = client.query(query, job_config=job_config)
query_job.result()

Complex SQL syntax

I have a game, and in the database I'm saving the user actions by date & time.
CREATE TABLE user_actions
(
aId BIGSERIAL PRIMARY KEY NOT NULL,
userId BIGINT NOT NULL REFERENCES users(uId) DEFERRABLE INITIALLY DEFERRED,
aDate TIMESTAMP without time zone DEFAULT now(),
aType INTEGER NOT NULL DEFAULT 0
);
My users are identified with email
CREATE TABLE users(
uId BIGSERIAL PRIMARY KEY NOT NULL,
uName VARCHAR (50) NOT NULL,
uEmail VARCHAR (75) UNIQUE NULL
);
and each day new prizes are added each day has a different number of prizes
CREATE TABLE prizes(
pId BIGSERIAL PRIMARY KEY NOT NULL,
pDate TIMESTAMP without time zone DEFAULT now(),
pType INTEGER NULL
pSize INTEGER NULL
);
This query list the userId and his last action date, per user
select distinct userId, max(aDate) from user_actions GROUP BY userId order by userId;
I want to create a query that will count the number of prizes added since each user last action.
I'm running:
OS: Debian
DB: Postgresql
code: Django
I think I will use CTE though It has not been tested
WITH last_actions AS (
SELECT DISTINCT userId, MAX(aDate) as last_logged
FROM user_actions
GROUP BY userId ORDER BY userId)
SELECT a.userId, COUNT(b.pDate)
FROM last_actions a, prizes b
WHERE b.pDate >= a.last_logged
GROUP BY a.userId;

How do i get the sqlite3 column

My code:
import sqlite3
conn = sqlite3.connect("keywords.db")
def search_location(name2):
name3 = ' '.join(name2)
c = conn.cursor()
c.execute('SELECT location FROM INPUT WHERE id = ?', (name3,))
for (location,) in c:
print name3.capitalize(),':' '\n',location
break
else:
pass # not found
Sqlite table:
sqlite>
CREATE TABLE input(
ID INT PRIMARY KEY AUTOINCRREMENT, NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY ....
);
I keep getting the error: No column LOCATION found. Items in name3 are to match the items in column NAME. If I change the code to read :
'SELECT NAME FROM INPUT WHERE id = ?', (name3,)
The error is the same. Please refer to the question I had asked earlier on How to improve sqlite database request and speed

How to parse SQL Queries and sub queries using sqlparser into python

Want to parse sql join query, select sub query into python. I am using sqlparse library. But i could not parse sub query. How i can parse whole query.
e.g:
query = "select id,fname,lname,address from res_users as r left join res_partner as p on p.id=r.partner_id where name = (select name from res_partner where id = 1)"
query_tokens = sqlparse.parse(query)[0].tokens
I could not parse for this select name from res_partner where id = 1 sub query.
Not so elegant, but works:
import sqlparse
from sqlparse.sql import Where, Comparison, Parenthesis
query = """
select
id,fname,lname,address
from
res_users as r
left join
res_partner as p
on
p.id=r.partner_id
where
name = (select name from res_partner where id = 1)"""
query_tokens = sqlparse.parse(query)[0]
where = next(token for token in query_tokens.tokens if isinstance(token, Where))
condition = next(token for token in where.tokens if isinstance(token, Comparison))
subquery = next(token for token in condition.tokens if isinstance(token, Parenthesis))
print subquery
prints:
(select name from res_partner where id = 1)
This library can parse and generate SQL https://code.google.com/p/python-sql/

How to check a table exists in sqlite3 or not

I am new to SQL. I want to check that a table exist in data base or not. If it does not exist than create it. Also I want to check that a record exist in table or not. If exists than update it with new data, If does not exist than insert a new record in table. Here is the code. I don't know how to check for a existing table,record.
import sqlite3 as lite
con = lite.connect('test.db')
cur = con.cursor()
con.execute('''CREATE TABLE EMPLOYEE
(ITEMA TEXT PRIMARY KEY NOT NULL,
ITEMB CHAR(50) NOT NULL,
ITEMC CHAR(50),
ITEM0 TEXT
ITEM1 TEXT
ITEM2 TEXT
ITEM3 TEXT
ITEM4 TEXT);''')
con.execute("INSERT INTO EMPLOYEE (ID,NAME,ADDRESS,MOBILE_NUMBER,TRANSPORT_FOR_LUNCH,ONLY_DROP,KIDS,ADULTS) \
VALUES (%s,%s',%s,%s,%s,%s,%s,%s)" %(ia,ib,ic,i0,i1,i2,i3,i4));
To test for table existence in sqlite, you can do something like:
cursor = con.cursor()
statement = "SELECT name FROM sqlite_master WHERE type='table';"
if (table_name,) in cursor.execute(statement).fetchall():
print "Table %s exists." % table_name
To update or create depending upon pre-existence of a record, sqlite allows the use of replace, which is an alias for insert or replace:
replace into table_name (columns) values (values)
CREATE TABLE EMPLOYEE IF NOT EXISTS ..
creates only if it does not exist