My first SQL database Im using sqlite3 in idle on linux. Everything is working fine but I want my table to display in an easier to read format in the terminal. Been reading about dot commands eg .mode column or .headers on but not sure were to put it in my code, I think this is the closest:
def veiw_outstanding_job():
cursorObj = con.cursor()
cursorObj.execute('.mode column\nSELECT * FROM '+ VesselName + ' WHERE State = "outstanding" ')
rows = cursorObj.fetchall()
for row in rows:
print(row)
menu()
but i get this error
Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code,self.locals)
File "/home/mike/Documents/Maintenance Manager/Maintenanceanager.py", line 238, in <module>
menu()
File "/home/mike/Documents/Maintenance Manager/Maintenance Manager.py", line 27, in menu
veiw_job(con)
File "/home/mike/Documents/Maintenance Manager/Maintenance Manager.py", line 54, in veiw_job
cursorObj.execute('.mode column\nSELECT * FROM '+ VesselName) sqlite3.OperationalError: near ".":
syntax error
(also asked on database admin stackexchange and got sent here)
Related
I have a trouble with my program in my raspberry pi. This is my source code
import MySQLdb
import csv
db=MySQLdb.connect(user='root',passwd='toor',
host='127.0.0.1',db='data')
cursor=db.cursor()
csv_data=csv.reader(file('datasensor.txt'))
for row in csv_data:
sql = "insert into `kelembapan` (`id`,`Tanggal`,`Tipe_sensor`,`Value`,`Ket`) values(%s,%s,%s,%s,%s);"
cursor.execute(sql,row)
db.commit()
cursor.close()
print "The Data has been inputted"
and this is for txt file
1, 2017-10-10, sensor1,40,Kurang lembap
2, 2017-10-10, sensor2,60,Lembap
That program can run in my ubuntu but not in my raspberry. when run in raspberry there is error
Traceback (most recent call last):
File "server.py", line 9, in <module>
cursor.execute(sql,row)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py",line 159, in execute
query = query% db.literal(args)
TypeError: not enough arguments for format strings
Thnks before :)
I am using a python module called phoenixdb to access phoenix which is SQL wrapper to query over HBase.
Here is my code snippet:-
import phoenixdb
database_url = 'http://localhost:8765/'
conn = phoenixdb.connect(database_url, autocommit=True)
cursor = conn.cursor()
cursor.execute("!table")
print cursor.fetchall()
cursor.close()
The phoenix query to list all the schemes and tables is !table or !tables.
But when I try passing the same in the execute function as shown above, I get the following error:-
Traceback (most recent call last):
File "phoenix_hbase.py", line 7, in <module>
cursor.execute("!table")
File "build/bdist.linux-x86_64/egg/phoenixdb/cursor.py", line 242, in execute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 345, in prepareAndExecute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 184, in _apply
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 90, in parse_error_page
phoenixdb.errors.ProgrammingError: ("Syntax error. Unexpected char: '!'", 601, '42P00', None)
Funny part is when I try to passing a different query, for example a select query, then script gets executed and produces result just fine.
Query:cursor.execute("select * from CARETESTING.EDR_BIN_SOURCE_3600_EDR_FLOW_SUBS_TOTAL limit 1")
Result:
[[2045,1023,4567]]
Is there any other format for passing !table which is equivalent to show tables in phoenixdb library's execute function which I am missing?
I tried looking up on the internet but unfortunately haven't come across anything helpful so far.
Thanks
!tables is sqlline grammar that can not be parsed by JDBC interface.
I'm trying update a row in a table but I'm getting an Argument Error.
Here is the code:
inventory_host = InventoryHost(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac, host_name=name)
try:
session.add(inventory_host)
session.commit()
except sqlalchemy.exc.IntegrityError:
session.execute(update(inventory_host))
session.commit()
session.close()
This is the error I'm getting:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "perception.py", line 77, in <module>
main()
File "perception.py", line 66, in main
modules.nmap_parser.parse_nmap_xml(nmap_xml)
File "/Users/arozar/Documents/Scripts_Code/Python-Projects/perception/modules/nmap_parser.py", line 128, in parse_nmap_xml
session.execute(update(inventory_host))
File "<string>", line 2, in update
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 668, in __init__
ValuesBase.__init__(self, table, values, prefixes)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 183, in __init__
self.table = _interpret_as_from(table)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/selectable.py", line 41, in _interpret_as_from
raise exc.ArgumentError("FROM expression expected")
sqlalchemy.exc.ArgumentError: FROM expression expected
The session.add(inventory_host) works for new hosts in the inventory_host table, but as soon as I try and update the row using session.execute(update(inventory_host)) I get the error.
update takes a table name as its first arg, not an instance of your table class. What value(s) do you want to update? If you wanted to update the host_name, for example, you might do this instead:
from sqlalchemy import update
# Ideally, just use your primary key(s) in your where clause; I'm not sure what they are
stmt = (update(InventoryHost).where(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac)
.values(host_name=name) # updates the host_name, for example
session.execute(stmt)
...
I have a working python script which retrieves data from a sqlite3 db. I need to convert it to talk to a mysql db though, and here's the first line that is giving me grief:
PRODUCT_CODE_ID = "SELECT id FROM localsite_productcode WHERE localsite_productcode.product_code=?"
cur.execute(PRODUCT_CODE_ID,(product_code,))
When I try that with mysql I get the following error:
Traceback (most recent call last):
File "gen-csv.py", line 85, in <module>
cur.execute(PRODUCT_CODE_ID,(product_code,))
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
What am I doing wrong here?
I don't think the DB API for MySQL supports the ? as a place holder - just try %s instead.
I'm trying to learn Django and following along the Django Book tutorial and I'm getting an error when I type these lines into the Python shell:
>>> from django.db import connection
>>> cursor = connection.cursor()
Here's the traceback I get:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 306,
in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
288, in _cursor
self._sqlite_create_connection()
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
278, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
OperationalError: unable to open database file
Any ideas on how to solve this? On my settings.py I have DATABASES ENGINE set to: django.db.backends.sqlite3 and NAME set to: C:\Python27/PythonProjects/mysite. Thanks!
I find (in modern Django) that just giving a filename like database.sqlite for the filename as the name is the best option. Django should interpret that by sticking the new database in the root folder of the project, which is perfect for my needs.