I have table name VenueSetting
id prefix type
------------------
1 456 1
2 8 2
3 4560 3
how to query on a input number to get matched the prefix in database using SqlAchemy ORM query, a version of like in reverse
something like this query
from_phone = '456789000'
session.query(VenueSetting).
filter(from_phone.like(VenueSetting.prefix + '%')).
first()
#expect result is record with id=1 in database
Related
I have an issue with my MySQL database. I am programming it in python.
I have 2 tables: Raspberry_data and Operation1.
I must read the data from Operation1 and copy some values from Operation1 to Raspberry_data table. The issue that some columns in Raspberry_data are identical which causes the query to work incorrectly.
Please check the following query:
http://sqlfiddle.com/#!9/a4c2ef/5
I must update Current_operation and ID columns in the Raspberry_data table from the data in Operation1.
The expected result:
Current_operation = 1 ID = 4
Current_operation = 1 ID = 6
However, the result is :
Current_operation = 1 ID = 4
Current_operation = 1 ID = 4
How can I ensure that it copies the individual rows line by line?
I am not able to execute this query for some reason on sqlfiddle but I have tested it on my actual mysql database and the results are the same.
My struggle is not with creating a table, I can create a table. The problem is to populate columns based off of calculations of other tables.
I have looked at How to create all tables defined in models using peewee and this is not helping me do summations and count etc..
I have a hypothetical database (database.db) and created these two tables:
Table 1 (from class User)
id name
1 Jamie
2 Sam
3 Mary
Table 2 (from class Sessions)
id SessionId
1 4121
1 4333
1 4333
3 5432
I simply want to create a new table using peewee:
id name sessionCount TopSession # <- (Session that appears most for the given user)
1 Jamie 3 4333
2 Sam 0 NaN
3 Mary 1 5432
4 ...
Each entry in Table1 and Table2 was created using User.create(...) or Sessions.create(...)
The new table should look at the data that is in the database.db (ie Table1 and Table2) and perform the calculations.
This would be simple in Pandas, but I cant seem to find a query that can do this. Please help
I found it...
query = Sessions.select(fn.COUNT(Sessions.id)).where(Sessions.id==1)
count = query.scalar()
print(count)
>>> 3
# Or:
query = Sessions.select().where(Sessions.id == 1).count()
3
For anyone out there : )
table A :
................
id name age |
................
1 G 29 |
2 A 30 |
................
table B : (table b have the foreign key of table A in tableA_id field which comes multiple)
id phone rank tableA_id
1 98989 A 1
2 98989 C 1
3 98989 D 2
table C : (table C have the foreign key of table A in tableA_id field which comes multiple)
id notes email tableA_id
1 98989 A#gmail.com 1
2 98989 C#gmail.com 1
In my case i am want to get all the data from all the tables and want to display in a single page . the what i want is i want one single query to get the all data from all of
three table with one query set.
And id what i am sending is Table_id = 1
so how can i get the data for table 1 from all the tables can anyone have idea please let me now i am a new be here
Well, probably you can't do it in single query. But using prefetch_related you can load all the related tables, so that DB hits will be reduced. For example:
# if the models are defined like this:
class TableA(models.Model):
name = models.CharField(...)
age = models.IntegerField(...)
class TableB(models.Model):
table_a = models.ForeignKey(TableA)
class TableC(models.Model):
table_a = models.ForeignKey(TableA)
# then the query will be like this
table_data = TableA.objects.filter(pk=1).prefetch_related('tableb', 'tablec')
for data in table_data:
print(data.name)
print(data.tableb.all().values())
print(data.tablec.all().values())
I am new to Python/Django.
I have two tables
tableA
id(pk) Name desc tableBID(fk)
1 ABC testdesc 1
2 XYZ testdes 2
tableB
id Name
1 firstName
2 Second Name
In Django, I have written below code
records = tableA.objects.all()
this is giving me tableA data but I need the TableB Name also.
Can anyone told me how to get the TableB name like
1 ABC testdesc 1 firstName
2 XYZ testdes 2 Second Name
You can use values to get required data with one query:
records = tableA.objects.values('Name', 'desc', 'tableBID__Name`)
tableBID__Name will fetch name from related TableB record.
Or to get list of objects instead of dictionaries use select_related:
records = tableA.objects.all().select_related('tableBID')
I'm working with python2.6 and MySQLdb. I have a table with this data
+----+--------+
| id | status |
+----+--------+
| 1 | A |
| 2 | B |
| 3 | B |
+----+--------+
I want to do an mysql update like this example:
UPDATE my_table SET status = "A" where id in (1,2,3,10001);
Query OK, 2 rows affected (0.03 sec)
Rows matched: 3 Changed: 2 Warnings: 0
And I need to know if all the ids in the update exits in the database. My idea to get this information was to compare the number of items I tried to update vs the number of matched rows. In the example the numbers are 4 vs 3.
The problem is that i don't know how to get the "Matched Rows" from the cursor information. I only see this information in cursor._info = 'Rows matched: 3 Changed: 2 Warnings: 0'.
The cursor.rowcount is the number of changed rows, so =(
Thanks!
If cursor._info contains that string, then you can just extract the 3 with a regex: re.search(r'Rows matched: (\d+)', cursor._info).group(1)
Alternatively, if you are using InnoDB tables (which support transactions), you can execute two queries: first just SELECT id FROM my_table WHERE id in (1,2,3,10001) and then get cursor.rowcount which will return the number of matching rows. Then execute your update. All queries run in the same cursors are part of the same transaction, so you are guaranteed that no other process will write the database between the queries.
Sources: see http://zetcode.com/databases/mysqlpythontutorial/
The FOUND_ROWS option makes cursor.rowcount return the number of matched rows instead:
db_connection = MySQLdb.connect(
host = settings['dbHost'],
user = settings['dbUser'],
passwd = settings['dbPass'],
db = settings['dbName'],
client_flag = MySQLdb.constants.CLIENT.FOUND_ROWS
)
Docs:
http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb.constants.CLIENT-module.html
http://dev.mysql.com/doc/refman/5.6/en/mysql-real-connect.html
(There's a typo in the MySQLdb docs. "client_flags" should be "client_flag")