I'm setting up an electronic system that reads electric current and send it into db. The time the system reads the electric current is different from when it's sent into the db.
I'm using Python so I use datetime.datetime.now(). How do I set the INSERT for it to work?
def create_table(self):
create_table_command = "CREATE TABLE sinal(id serial PRIMARY KEY, name varchar(100), medida float, data arr[] )"
self.cursor.execute(create_table_command)
def insert_new_record(self, label, measure, time):
new_record = (label, measure, time)
insert_command = "INSERT INTO sinal(name, medida, data) VALUES('" + new_record[0] + "','" + new_record[1] + "','" + new_record[2] + "')"
pprint(insert_command)
self.cursor.execute(insert_command)
This is the error I get:
psycopg2.errors.UndefinedObject: type "arr[]" does not exist LINE 1:
...l PRIMARY KEY, name varchar(100), medida float, data arr[] )
Related
I got an error in the 5th line in my code below at '"+uname+"'.
How can I create a table at runtime?
Here is my code :
name = en1.get()
uname = en2.get()
password = en3.get()
sql = "insert into register values ('" + name + "','" + uname + "','" + password + "')"
CreateTable = "CREATE TABLE '"+uname+"'(no INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),amount INT,date DATE,mode VARCHAR(255))"
try:
cur.execute(sql)
cur1.execute(CreateTable)
con.commit()
con1.commit()
messagebox.showinfo("Success", "Your data is registered successfully!")
except:
messagebox.showinfo("Error inserting", "Please change your username and try.")
In the statements with get(), if you get None as a return value then the below statements will fail
name = en1.get() #-- None
sql = "insert into register values ('" + name + "','" + uname + "','" + password + "')"
CreateTable = "CREATE TABLE '"+uname+"'(no INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),amount INT,date DATE,mode VARCHAR(255))"
Try this instead so that if any of the values is None, then below output will be obtained
without causing the error but still the sql query needs to be handled
sql = "insert into register values ('{}','{}','{}')".format(name, uname, password)
CreateTable = "CREATE TABLE '{}'(no INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),amount INT,date DATE,mode VARCHAR(255))".format(name)
print("sql is ", sql)
print("sql is ", CreateTable)
# Output
sql is insert into register values ('None','None','None')
sql is CREATE TABLE 'None'(no INT AUTO_INCREMENT PRIMARY KEY,title VAR
CHAR(255),amount INT,date DATE,mode VARCHAR(255))
I have been trying to set the ObjectID as Identity column using python on SQL. The below SQL statment works on Management Studio (SSMS) and sets the ObjectID as Identity column. The code works, but when checking the table on SSMS, I don't see ObjectID as Identity column.
The following ways work on python but do not change the identity column on SSMS.
Adding conn.commit() after each execution.
Running the .sql file with python file reader.
Shown in the image, the Identity column is still empty after code execution in python.
The code was generated by SSMS, and my purpose is to SET ObjectID field Identity column. Maybe there is a better way.
Here is the code:
newTableName = "A_Test_DashAutomation"
try:
cursor.execute("""
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION """)
cursor.execute("""
CREATE TABLE dbo.Tmp_""" + newTableName + """
(
SubProjectTempId bigint NULL,
CIPNumber varchar(16) NOT NULL,
Label nvarchar(50) NULL,
Date_Started datetime2(7) NULL,
Date_Completed datetime2(7) NULL,
Status nvarchar(25) NULL,
Shape geography NULL,
Type varchar(2) NOT NULL,
ProjectCode varchar(16) NULL,
ActiveFlag int NULL,
Category varchar(32) NULL,
ProjectDescription varchar(64) NULL,
UserDefined varchar(1024) NULL,
InactiveReasonDate datetime NULL,
FYTDBudget money NULL,
LTDBudget money NULL,
PeriodExpenses money NULL,
FYTDExpenses money NULL,
LTDExpenses money NULL,
LTDEncumbrances money NULL,
LTDBalance money NULL,
FiscalYear int NULL,
ToPeriod int NULL,
_LastImported datetime NOT NULL,
OBJECTID int NOT NULL IDENTITY (1, 1),
GDB_GEOMATTR_DATA varbinary(MAX) NULL
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
""")
cursor.execute("ALTER TABLE dbo.Tmp_" + newTableName + " SET (LOCK_ESCALATION = TABLE)")
cursor.execute("SET IDENTITY_INSERT dbo.Tmp_" + newTableName + " ON")
cursor.execute("""
IF EXISTS(SELECT * FROM dbo.""" + newTableName + """)
EXEC('INSERT INTO dbo.Tmp_""" + newTableName + """ (SubProjectTempId, CIPNumber, Label, Date_Started, Date_Completed, Status, Shape, Type, ProjectCode, ActiveFlag, Category, ProjectDescription, UserDefined, InactiveReasonDate, FYTDBudget, LTDBudget, PeriodExpenses, FYTDExpenses, LTDExpenses, LTDEncumbrances, LTDBalance, FiscalYear, ToPeriod, _LastImported, OBJECTID, GDB_GEOMATTR_DATA)
SELECT SubProjectTempId, CIPNumber, Label, Date_Started, Date_Completed, Status, Shape, Type, ProjectCode, ActiveFlag, Category, ProjectDescription, UserDefined, InactiveReasonDate, FYTDBudget, LTDBudget, PeriodExpenses, FYTDExpenses, LTDExpenses, LTDEncumbrances, LTDBalance, FiscalYear, ToPeriod, _LastImported, OBJECTID, GDB_GEOMATTR_DATA FROM dbo.""" + newTableName + """ WITH (HOLDLOCK TABLOCKX)')
""")
cursor.execute("SET IDENTITY_INSERT dbo.Tmp_" + newTableName + " OFF")
cursor.execute("DROP TABLE dbo." + newTableName)
cursor.execute("EXECUTE sp_rename N'dbo.Tmp_" + newTableName + "', N'" + newTableName + "', 'OBJECT'")
cursor.execute("""
ALTER TABLE dbo.""" + newTableName + """ ADD CONSTRAINT
R1143_pk PRIMARY KEY CLUSTERED
(
OBJECTID
) WITH( PAD_INDEX = OFF, FILLFACTOR = 75, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
""")
cursor.execute("""
CREATE SPATIAL INDEX SIndx ON dbo.""" + newTableName + """(Shape) USING GEOGRAPHY_AUTO_GRID
WITH( CELLS_PER_OBJECT = 16, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
""")
cursor.execute("""
ALTER TABLE dbo.""" + newTableName + """ ADD CONSTRAINT
g1084_ck CHECK (([Shape].[STSrid]=(4326)))
""")
cursor.execute("COMMIT")
conn.commit()
print("Object ID is set to Identify column.")
except pymssql.DatabaseError, err:
print(str(err))
Any help would be appreciated.
Thanks
Problem solved. The STSrid changes everytime you create a new table, so taking this portion out of the SQL code helped. However, I am not sure if the spatial index remained the same.
Cheers
cursor.execute("""
ALTER TABLE dbo.""" + newTableName + """ ADD CONSTRAINT
g1084_ck CHECK (([Shape].[STSrid]=(4326))) """)
I want to measure the total time consumed by PostgresSQL to execute a series of triggers.
In short words, the system is a python program who used the DB to store and calculate the shortest path using some plugins installed in postgresql. The system depends on triggers which called once a request is inserted in the request table.
for i in range(50):
readyhostslist = random.sample(allhosts, 100)
fid = 0
fw = 0
hostlistready = {}
for n in xrange(0,len(readyhostslist), 2):
hostlistready[readyhostslist[n][0]] = readyhostslist[n+1][0]
for src, dst in hostlistready.iteritems():
try:
# get next flow id
#print src, dst
self.db.cursor.execute("SELECT * FROM rm;")
fid = len(self.db.cursor.fetchall()) + 1
self.db.cursor.execute("INSERT INTO tracktime (fid) VALUES ({0} )".format(fid))
self.db.cursor.execute("INSERT INTO rm (fid, src, dst) VALUES ({0}, {1}, {2});".format(fid, src, dst))
self.db.cursor.execute("UPDATE rm set FW = {0} where fid = {1};".format(fw, fid))
self.db._conn.commit()
except Exception, e:
print "Failure: flow not installed --", e
what I want to do is to know the total elapsed time from the moment I insert the request using python postgresql driver to the end where the results are stored in other table.
I tried the following but I failed. I start to insert current time before inserting the request and modify the last called trigger to update the end time.
DROP TABLE IF EXISTS tracktime CASCADE;
CREATE UNLOGGED TABLE tracktime (
id serial ,
fid integer,
timestart timestamp default clock_timestamp(),
timeend timestamp ,
waqtstart timestamp ,
waqtend timestamp ,
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS p_routing CASCADE;
CREATE UNLOGGED TABLE p_routing (
counts integer,
status text,
PRIMARY key (counts)
);
CREATE TRIGGER run_routing_trigger
AFTER INSERT ON p_routing
FOR EACH ROW
EXECUTE PROCEDURE spv_constraint1_fun();
CREATE OR REPLACE RULE run_routing AS
ON INSERT TO p_routing
WHERE (NEW.status = 'on')
DO ALSO (
UPDATE p_routing SET status = 'off' WHERE counts = NEW.counts;
);
DROP TABLE IF EXISTS p_{0} CASCADE;
CREATE UNLOGGED TABLE p_{0} (
counts integer,
status text,
PRIMARY key (counts)
);
CREATE OR REPLACE RULE run_{0} AS
ON INSERT TO p_{0}
WHERE (NEW.status = 'on')
DO ALSO (
DELETE FROM {1};
UPDATE p_{0} SET status = 'off' WHERE counts = NEW.counts;
);
CREATE OR REPLACE FUNCTION spv_constraint1_fun ()
RETURNS TRIGGER
AS $$
plpy.notice ("spv_constraint1_fun")
if TD["new"]["status"] == 'on':
rm = plpy.execute ("SELECT * FROM rm_delta;")
flowid = 0
for t in rm:
if t["isadd"] == 1:
f = t["fid"]
flowid = f
s = t["src"]
d = t["dst"]
pv = plpy.execute("""SELECT array(SELECT id1 FROM pgr_dijkstra('SELECT 1 as id, sid as source, nid as target, 1.0::float8 as cost FROM tp WHERE isactive = 1',""" +str (s) + "," + str (d) + ",FALSE, FALSE))""")[0]['array']
plpy.execute ("UPDATE tracktime SET timeend = clock_timestamp() where fid =" + str (flowid) +";")
l = len (pv)
plpy.execute ("INSERT INTO trackzeit (fid) VALUES(" + str (flowid) +");")
for i in range (l):
if i + 2 < l:
plpy.execute ("INSERT INTO cf (fid,pid,sid,nid) VALUES (" + str (f) + "," + str (pv[i]) + "," +str (pv[i+1]) +"," + str (pv[i+2])+ ");")
plpy.execute ("UPDATE trackzeit SET zeitend = clock_timestamp() where fid =" + str (flowid) +";")
elif t["isadd"] == 0:
f = t["fid"]
plpy.execute ("DELETE FROM cf WHERE fid =" +str (f) +";")
plpy.execute ("DELETE FROM rm_delta;")
return None;
$$ LANGUAGE 'plpythonu' VOLATILE SECURITY DEFINER;
CREATE TRIGGER spv_constraint1
AFTER INSERT ON p_spv
FOR EACH ROW
EXECUTE PROCEDURE spv_constraint1_fun();
while this works for individual requests, it doesn't when I try to loop and inserts 100 requests. What I observed is the python driver or the DB inserts the 100 requests then trigger the functions for each request.
p.s. I tried auto commit after each insertion statement in the python program and still got the same problem.
This is a quote from the results where you can see the cumulative or nearly cumulative(some other trials will have not this cumulative phenomena). Each line represents the total time measured using PostgreSQL functions associate with flow number (fid)
77.625
130.395
168.6
208.572
239.531
273.252
295.924
320.999
349.042
369.552
398.883
432.04
462.708
498.156
525.315
565.321
601.193
636.402
667.983
695.893
731.778
759.155
792.391
814.026
842.942
874.946
917.369
934.897
963.269
How can I set a primary key in psycopg2 from a tuple of values being looped in from a tuple?
for example i have my tuple
meetattrs = ('id', 'venue', 'date', 'rail', 'weather', 'trackcondition')
and then I want to create the table and extract and insert values. From the meetattrs I wish to set id as the primary key.
conn = psycopg2.connect("")
with conn, conn.cursor() as cur:
# First, create tables.
cur.execute("drop table if exists meetings, races, horses")
cur.execute("create table meetings (id integer PRIMARY KEY, " +
", ".join("%s varchar" % fld for fld in meetattrs)
+ ")")
This however creates this error, unsure how to resolve.
Traceback (most recent call last):
File "racemeeting.py", line 56, in <module>
+ ")")
psycopg2.ProgrammingError: column "id" specified more than once
Your current sql query after the join is performed and the placeholders take their values is:
'create table meetings (id integer PRIMARY KEY, id varchar, venue varchar, date varchar, rail varchar, weather varchar, trackcondition varchar)'
id is specified twice.
You can build your query in a much cleaner way:
query_params = " %s integer PRIMARY KEY" + ", %s varchar" * (len(meetattrs) - 1)
And your SQL statement becomes:
"create table meetings (" + query_params % meetattrs + ")"
After the insertion of placeholder values:
'create table meetings (id integer PRIMARY KEY, venue varchar, date varchar, rail varchar, weather varchar, trackcondition varchar)'
Be sure your tuple always has its first item as id
In that kind of problem, you should first try to build and control the sql string.
("create table meetings (id integer PRIMARY KEY, " +
", ".join("%s varchar" % fld for fld in meetattrs)
+ ")")
gives:
'create table meetings (id integer PRIMARY KEY, id varchar, venue varchar, date varchar, rail varchar, weather varchar, trackconditionvarchar)'
effectively duplicating id column.
The solution is then trivial: remove first identifier from meetattrs:
cur.execute("create table meetings (id integer PRIMARY KEY, " +
", ".join("%s varchar" % fld for fld in meetattrs[1:])
+ ")")
Alternatively if you cannot be sure that the primary key if the first element:
cur.execute("create table meetings (id integer PRIMARY KEY, " +
", ".join("%s varchar" % fld for fld in meetattrs if fld != 'id')
+ ")")
I'm completely new to databases, and have put something simple together using the helpful guide that can be located at http://halfcooked.com/presentations/osdc2006/python_databases.html , However it's returning an error that I don't understand
try:
from sqlite3 import dbapi2 as sqlite
except ImportError:
from pysqlite2 import dbapi2 as sqlite
db_connection = sqlite.connect('program.db')
db_curs = db_connection.cursor()
def create_customer(cID, fname, sname, dob):
db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_curs.execute("INSERT INTO " + cID + " (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
db_connection.commit()
db_curs.execute("SELECT * FROM " + cID )
create_customer("1", "John", "Farnham", "12/08/95")
create_customer("1", "Indianna", "Jones", "05/05/95")
print db_curs.fetchall()
the error I am receiving is as follows:
Traceback (most recent call last):
File "C:\Users\fin0005\Documents\loyalty.py", line 17, in <module>
create_customer("1", "John", "Farnham", "12/08/95")
File "C:\Users\fin0005\Documents\loyalty.py", line 12, in create_customer
db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
OperationalError: near "1": syntax error
Add backticks around your table name, so that it doesn't think it's creating an integer as a table name
def create_customer(cID, fname, sname, dob):
db_curs.execute("CREATE TABLE `" + cID + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_curs.execute("INSERT INTO `" + cID + "` (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
db_connection.commit()
db_curs.execute("SELECT * FROM `" + cID + "`")
# In SQL terms, the following blows up
# create table 2 (id int(10) PRIMARY KEY); Due to the 2 being an integer
# create table `2` (id int(10) PRIMARY KEY); Works, due to the 2 being properly identified with backticks :)
# Here's some code as requested in the comment, everything below this point is a self contained example, please do not copy the function above
def initiate_customer_table(table_name):
db_curs.execute("CREATE TABLE IF NOT EXISTS `" + table_name + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
db_connection.commit()
def create_customer(table_name, fname, sname, dob):
db_curs.execute("INSERT INTO `" + table_name + "` (first_name, last_name, date_of_birth) VALUES (%s, %s, %s)", [fname, sname, dob])
db_connection.commit()
# Fetches the user just created
db_curs.execute("SELECT * FROM `" + table_name + "` WHERE id = %s", [db_curs.insert_id()])
# Returns the user
return db_curs.fetchone()
desired_table_name = 'customers'
initiate_customer_table(desired_table_name)
customer_1 = create_customer(desired_table_name, "Bryan", "Moyles", "1800-01-01")
customer_2 = create_customer(desired_table_name, "Awesome", "Guy", "1800-01-01")
I will also suggest that you go a step further, if you plan on using this code in production, to ensure that all fields are escaped properly for mysql.