Replace the table name in query with variabale value
-
Is it possible to replace the table name in query with variabale value?
-
@dvlpr-bernard Sure:
QString queryStrTemplate = "SELECT * from %1"; c.execute(queryStrTemplate.arg("TABLE_NAME"));
-
@dvlpr-bernard
Note that as @jsulm has written, for a table name (which is not shown as a variable in your code) you cannot do it via the SQL?
-binding mechanism, you must do it as he has shown by literal in-line substitution as you build the statement. -
@dvlpr-bernard You're using a Python string. So, simply use what Python provides you:
queryStrTemplate = 'SELECT * from {}' c.execute(queryStrTemplate.format(table_name))
-
@dvlpr-bernard
In addition to what @jsulm has corrected, once you get past that line you will need to correct your Python code when it gets to your subsequent line (oh, please try to paste code not screenshots, it makes it impossible for respondents to copy/paste!)c.execute('... {} \'.format(...) where SCHED_ID = ?', (1,)))
It might help if you read up on Python strings.
I also observe that since you are choosing to do all your database access through Python library calls, there is not a single bit of Qt or PyQt in the code you have now pasted. That's fine, but then for generic Python questions you might find a Python forum more suitable than a Qt one.
-
Thank you for your response :)
But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.table_name = '1.0' queryStrTemplate = 'SELECT NO_EXERCISE from {}' c.execute(queryStrTemplate.format(table_name)) c.execute('select NO_EXERCISE from {} \'.format(table_name) where SCHED_ID = ?', (1,)) row = c.fetchone() print(row[0])
-
@dvlpr-bernard said in Replace the table name in query with variabale value:
I'm not familiar with the syntax of python
Then you should learn it.
And please take a closer look at your execute() line it is really broken...c.execute('select NO_EXERCISE from {} where SCHED_ID = {}'.format(table_name, 1))
-
@dvlpr-bernard said in Replace the table name in query with variabale value:
But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.
We (I) do not want to be rude or mean to you. But if you are not familiar with Python or its syntax, why are you programming in it, how do you expect to get anywhere? You are going to get this kind of issue on every line you try to write. Which is also why I said you might want to use a general Python programming forum, since this sort of thing simply has nothing to do with Qt, but as you please.
-
This post is deleted!
-
I didn't meant to offend you guys. I was working on a PyQt app it is just a small school project. Python is the required language, what do I mean is I don't have time to dig deeper on learning other python stuff. I learned the fundamentals and I also interested on the things that I needed. But in this case, I was hyped because I was about to finish my project. The only thing left is replacing the table name in query with variable value and then my project will get finished. That's maybe the reason why I was hyped to get the correct syntax for that.
I apologize if I said something mean, English is not my first language. I hope you understand. Thank you for both of you :)
-
@dvlpr-bernard
I finally noticed the problem. It must betable_name = "\'1.0\'"
not
table_name = '1.0'
Thank you guys for your help :)
-
@dvlpr-bernard
One thing I meant to ask: what editor do you use when writing your Python code? If it's an IDE like PyCharm or VSCode it will helpfully show you when code is syntactically wrong, offer you methods/parameters to choose from etc.; if you are not using an editor with Python syntax you are missing out. -
@Denni-0 Thank you I will take note of that
-
@dvlpr-bernard
I imagine IDLE, being a Python editor, shows you errors, no? The point was, in your earlier erroneous code, I would have expected your editor to be showing you those lines were incorrect. -
@JonB HAHAHAHAHAH it works fine to me, IDLE also shows where the error is, but not as powerful as other IDE's can do. It would have been a little bit easier if I use other IDE's like PyCharm. But just like I what I have said I had so much fun and excitement using IDLE. Next time I would prob use PyCharm to put myself at ease.
-
@Denni-0 I'm sorry sir. I felt bad about the replies(they were indirectly telling me that I don't belong or I shouldn't be here), so I decided to delete it. Forget about it, I appreciate your help sir :).