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')
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 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().
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'm stuck here, I wonder how I can transfer excel files with data to my database through python? Appreciate answers.
My local host is called DB_01<br>
My database is called dbo.test1
My tables contain:
a - int
b - int
c - int
d - int
e - int
f - int
Would it be okay to use Panda's here?
This is possible. It is hard to answer your question with such little information, but I suggest you to look at using the dataframe to_sql method of pandas. This also requires creating a connection to your database with sqlalchemy.
Something along the lines of:
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mssql+pymssql://scott:tiger#hostname:port/dbname', echo=False)
df = pd.read_csv("your_file.csv")
df.to_sql('your_table_name', con=engine)
The above is just pseudocode. Check out the documentation on setting up a sqlalchemy connection here.
Thank you wfbbrown :)
Will the code by the same if my files is xlsx(shift csv to xlsx)?
I hope this can explain more( I use SQL Microsoft server)
My database look like this: ID Block BlockSell BlockBuy RejectedBlock Netimports
And I want have data under each elementes.
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 7 years ago.
Improve this question
Hi my project works on Django Python. What I want to achieve is since django tasty pie doesn't support combining two resources(no relationship b/w the tables), I need to come up with my own resource. Here I have 2 tables, Table A and B. There is no relationship b/w these 2 tables. But both the tables have a field/column named gname in common. So I want to get all the distinct gnames from both the tables and put it into one list (no duplicated values) and I need to display these gnames as a list in my template. Is there anyway to do that? Thanks in advance.
try:
gnames1 = list(A.objects.values_list('gname',flat=True).distinct())
gnames2 = list(B.objects.values_list('gname',flat=True).distinct())
gnames = list(set(gnames1+gnames2))
render(request, 'sampletemplate.html', {'gnames':gnames})
Set is a data structure that doesn't allow duplicate values.
You can add all the values from both the tables to it and I think you are good to go.
Python Sets
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 8 years ago.
Improve this question
Let's say I have a database called "A", this database has only two records "name: Jhon" and "name: Sara", and I also have the database "B" that has the names "Jhon" and "Sara" among other names, how can I search the database B only for the names Jhon and Sara?
Context: The database A has specific values, the database B is user generated, what I'm trying to accomplish is when the user-generated record to B database matches the A's record add some points to the user, it's like a game or something.
How can I accomplish this?
Generally, a django model maps to a single database table.
Each attribute of the model represents a database field.
the following example retrieve a list of names from the first Model (named "A"), then searches into "B" for any record having the value of name attribute in that list.
names_to_search = A.objects.all().values_list('name', flat=True)
people = B.objects.filter(name__in=names_to_search)
I assume that both A and B have a field called name.
A database has tables, which have attributes.
In this case, you have 2 tables A and B.
You have "name" attribute for each table.
I do not understand what you are trying to do, but if you want to query a table with a name in Django,
person = A.objects.get(name=myName)
So if you want to know if Jhon is in table A,
try:
person = A.objects.get(name="Jhon")
#Jhon exists.
except ObjectDoesNotExist:
#Jhon does not exists.