Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am using the mysql.connector python library to connect to mysql. I need to display a table present in my database in a python program using the pyqt5 GUI framework.
This is me trying to print out the number of rows in my tables but even though i have 3 rows its showing up 0.
cursor=mydb.cursor()
command = "select * from MENU"
result = cursor.execute(command)
self.tableWidget.setRowCount(cursor.rowcount)
print(cursor.rowcount)
That is because you haven't fetched anything from the cursor yet.
As the docs say:
For nonbuffered cursors, the row count cannot be known before the rows have been fetched. In this case, the number of rows is -1 immediately after query execution and is incremented as rows are fetched.
So to fix you can get the cursor like this: cursor=mydb.cursor(buffered=True) or you can fetch all the results with something like results = cursor.fetchall().
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have a large SQL script which is too large to run from SQL Server Management Studio.
Is there a way to run such a script using Python?
SQLite3 doesn't seem to work. I keep getting syntax errors but the query is standard SQL.
Is there an MSSQL equivalent library in Python?
Use cursor from a pyodbc connection.
cursor.execute(<sql code as string>)
cursor.commit()
If you use sqlalchemy, you can get the cursor from engine as follows:
engine.raw_connection().cursor()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been trying to create a program that registers and logs in a user, the register part should store the details into a database but I've experienced a bit of trouble with taking input from the QT text box and inserting it into my database. It doesn't take any input from the text box, so I've tried to use qtsql to help me input data, but I can't manage to get it working.
#Register
def InsertData(self):
db=QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("/home/user/test.db")
if db.open():
query=QSqlQuery
query.exec_("INSERT INTO users(email_address,password) VALUES(?,?)",self.password.text,self.email.text)
db.commit()
db.close()
You can‘t get the value in your SQL statement, because you are not calling the object for password and email
Try:
val_password = self.password.text()
val_email = self.email.text()
query.exec_("INSERT INTO users(email_address,password) VALUES(?,?)",val_password, val_email)
I‘m not used to qtsql, but in sqlite you also have to assign a cursor to your sql connection, you might also want to check that.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
What sql query can be used to search for a variable in a table, with the aim that the variable isn't there. How do I know if a value is in my table? Because I haven't found a search query yet.
I tried making the sql query a varible and setting it to true/false but that didn't work.
I created the table in sqlite it has the following fields ('CustomerID', 'UserName', 'FirstName', 'Password', 'Email') . I'm doing validation and I want to check if a username has already been used.
You could use a SQL check something like...
SELECT 1 AS userExists
FROM yourTable
WHERE userName = 'JoeBloggs'
LIMIT 1
(Return the value 1 for each row where the username is JoeBloggs, but limit the results to just one row.)
Then, in the python, check how many rows it returns. 0 rows = it doesn't exist, 1 row = it exists at least once.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table named 'student_info' in a Postgres database. The table has coloumns
'student_id', 'student_name', and 'student_age'.
I have written three Python functions namely : student_id(), student_name(), student_age() returning student_id, student_name, and student_age respectively.
I want to write these values into respective columns in the table from SQLAlchemy.
I am not getting any information on how can I do it.
If you are beggining with sqlalchemy you can follow the tutorial, it will guide you to every step of connecting to your database, create mapping entity, and perform basic operations on your tables with sqlalchemy.
if you already know how to connect and have a mapped entity to represent your table you can simply instantiate your models, add data into each fields then use the add method like this:
student = Student(student_id=1, student_name='foo', student_age=15)
session.add(student)
session.commit()
if you dont want to use sqlalchemy mapped entity you can perform raw sql using an sqlalchemy engine:
db_url = 'your_database_url'
engine = create_engine(db_url)
engine.execute('your_sql_query')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can I access pre-existing SQL databases with this module? If not, what's a good third-party module for this kind of thing?
I just need to be able to pick up 2 columns from a table that is updated every day.
Please let me know if there's anything I should clarify. Thanks!
EDIT: I noticed that I wasn't asking about the right thing... I'm very unfamiliar with SQL and the one I use is connected to a server that needs login info and a host... can someone remove this question?
This is what the first parameter of the connect function is for:
import sqlite3
db = sqlite3.connect("C:/temp/MyLittleDatabase")
db.execute("CREATE TABLE IF NOT EXISTS T(x)")
db.execute("INSERT INTO T VALUES (42)")
cursor = db.execute("SELECT x FROM T")
for row in cursor:
print row[0]