I'm able to connect with my database. But I want to change the values present in that particular column.
cursor.execute("SELECT * FROM foo;")
rows = cursor.fetchall()
for row in rows:
print(row)
Output of the above code is,
('foo,bar,foo,bar',)
('foobar,bar',)
('foo,bar,buz,buz',)
I'm able to replace the value by,
rows = cursor.fetchall()
for row in rows:
print(re.sub(r'^[^,]*,', '', row[0]))
cursor.close()
returns,
bar,foo,bar
bar
bar,buz,buz
but I don't know how to setback the altered string to that particular column.
I think i need to use update query, so I tried
for row in rows:
cursor.execute("UPDATE foo SET categories=%s;", re.sub(r'^[^,]*,', '', row[0]))
But it returns an error message of,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
As i said in comment you need to specify the column ID for your query :
for row in rows:
sub=re.sub(r'^[^,]*,', '', row[0])
cursor.execute("UPDATE drug_specifications SET categories=%s where id=%s;",(sub, str(row[0])))
Related
I'm working on a project where I need to get data from my SQL Server, but there is a catch. In total there is around 100.000 rows in the specific column I need the data out of but I only need the last 20.000 - 30.000 rows of it.
I use the casual connection string and stored procedure but is there a way to select a specific row to start from? (for example let it start at row 70.000)
try:
CONNECTION_STRING = 'DRIVER='+driver+';SERVER='+server+';DATABASE='+databaseName+';UID='+username+';PWD='+ password
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
storedproc = "*"
cursor.execute(storedproc)
row = cursor.fetchone()
while row:
OID = ((int(row[1])))
print(OID)
So my question: is there a way (for example) set cursor.fetchone to row 70.000 instead of 1? Or is there another way to do that?
Thanks in advance!
trying to insert values into one MySQL table using python.
First inserting values from csvfile; with:
sql = "INSERT INTO mydb.table(time,day,number)values %r" % tuple (values),)
cursor.execute(sql)
then insert into the same table and same row an other value
sql = "INSERT INTO mydb.table(name) values(%s)"
cursor.execute(sql)
with this i get the inserts in two different rows…
But i need to insert it into the same row without using sql = "INSERT INTO mydb.table(time,day,number,name)values %r" % tuple (values),)
Is there a way to insert values into the same row in two 'insert statements'?
INSERT will always add a new row. If you want to change values in this row, you have to specify a unique identifier (key) in the WHERE clause to access this row and use UPDATE or REPLACE instead.
When using REPLACE you need to be careful if your table contains an auto_increment column, since a new value will be generated.
I'm creating a program where I need to check if a certain cell in a table equals a string value and, if it does not, to not change that value. Here is some snippet of the code for clarification:
if (db.execute("SELECT :rowchosen FROM userboard WHERE column=:columnchosen", rowchosen = rowchosen, columnchosen = columnchosen)) == '-'):
#change value of cell
else:
#go to a new page that displays an error
Yet, whenever I run this code, I always get an error because the value (I believe) prints as a dictionary value, something like {"row" = 'row'} of that sort. Any help/advice as to why this happens?
Are you sure that userboard is the database and not the table?
i think, here is what you want to do
conn = sqlite3.connect(db_file)
cur = conn.cursor()
cur.execute("SELECT * FROM userboard WHERE one=?", (columnchosen,))
rows = cur.fetchall()
for row in rows:
print(row)
now, in the loop for row in rows: you need to perform your check. For all the rows returned, you need to check each row for - in the appropriate column
also check out http://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/
To sum it up. I have data in a row of a db (MySQL). This data are used in the script to calculate a value. A for loop is used to calculate this value for each row in the table.
The Problem: The results stored in the variable meant have to be updated in a different column of the same row.
I tried to include the update command in the for loop but I get an error (see below). If I do not include the update-command in the for loop I just get the result of the last row but for all rows!
The script:
for row in rows:
for col in row:
x = map(str.strip,col.lower().split())
st = map(lambda wo: abo.get(wo, 0), x)
meant = numpy.meant(st)
cur.execute("""UPDATE table_x SET result = %s Where text = %s""",(meant, rows))
Unfortunately I am getting this error:
Programming Error: (1064, 'You have an error in your SQL syntax; check the manual...
How can I update the column with each calculated value (meant) of that row?
As I see rows is a list ( or an iterable )
and in your query, you are binding the placeholder with rows ( instead of row? )
cur.execute("""UPDATE table_x SET result = %s Where text = %s""",(meant, rows)
That would most probably create a syntax error.
I am new in python so in the pyodbc. Maybe my question is very simple but I could not find any answer refer to my question.
I'm using this select
cursor.execute("SELECT [something] FROM [someone] WHERE [user_name]='John'")
rows = cursor.fetchall()
for row in rows:
print row.something
It prints some parameters for example 4 or 5.How to print only second or only third parameter.
I also used cursor.fetchmany() but I'm having same problem
If you wan't just the 4th row you can do:
rows = cursor.fetchall()
print rows[3].something
But it's better if you do it in the SQL query and avoid fetching all the rows from the database:
SELECT [something] FROM [someone] WHERE [user_name]='John' LIMIT 1 OFFSET 3
Example.
I guess your mean field and not parameter
cursor.execute("SELECT [something] FROM [someone] WHERE [user_name]='John'")
rows = cursor.fetchall()
from row in rows:
print row[1]