Inserting two lists into sqllite db - python

Working on a GUI app with Tkinter and I've collected information via a form into two lists. There is a reason behind why two lists, although there might be a better way.
So now I've got two lists, both contain data I need to enter into my database and each data point is a new row. For example, my gui is an equipment signout program.
So here is an example:
equipmentIdList = [123, 4568, 9876]
Note: equipmentIdList is simply barcodes ids identifying the data in my db
assignedToList = [rob, chris, mike]
So barcode for equipment 123 is assigned to rob and is going into the database to mark it as being checked out. Same for chris and mike.
So where I'm at is how to get this into my database. I did try going to a dictionary but that doesn't seem to really solve my problem either.
Thanks in advance.

You may save your data in a table with 2 columns: person and equipment
(rob, 123)
(chris, 4568)
(mike, 9876)
First you should design and create your table in sqlite, then read document how to generate insert query into your database.

Related

Storing multiple values in one column

I am designing a web application that has users becoming friends with other users. I am storing the users info in a database using sqlite3.
I am brainstorming on how I can keep track on who is friends with whom.
What I am thinking so far is; to make a column in my database called Friendships where I store the various user_ids( integers) from the user's friends.
I would have to store multiple integers in one column...how would I do that?
Is it possible to store a python list in a column?
I am also open to other ideas on how to store the friendship network information in my database....
The application runs through FLASK
What you are trying to do here is called a "many-to-many" relationship. Rather than making a "Friendships" column, you can make a "Friendship" table with two columns: user1 and user2. Entries in this table indicate that user1 has friended user2.
It is possible to store a list as a string into an sql column.
However, you should instead be looking at creating a Friendships table with primary keys being the user and the friend.
So that you can call the friendships table to pull up the list of friends.
Otherwise, I would suggest looking into a Graph Database, which handles this kind of things well too.
If you want to organize correct storage of data you should know more about relative databases. I recommend you to read this first of all. With some normalization it would perform better (some operations on db will be much more simplier).
As mentioned before you should make another table with friendships to perform first normal form. It would be much easier for you to perform modification of relationships.

How to create a large number of tables

I've got a large dataset to work with to create a storage system to monitor movement in a store. There's over like 300 products in that store and the main structure of all tables is the same. The only difference is the data inside. There's a larger data base called StorageTF and I want to create a lot of tables called Product_1,Product_2,Product_3 etc..
The table structure should look like
The main large data set (table) looks like this:
CREATE TABLE StoringTF (
Store_code INTEGER,
Store TEXT,
Product_Date TEXT,
Permission INTEGER,
Product_Code INTEGER,
Product_Name TEXT,
Incoming INTEGER,
Unit_Buying_Price INTEGER,
Total_Buying_Price INTEGER,
Outgoing INTEGER,
Unit_Sell_Price INTEGER,
Total_Sell_Price INTEGER,
Description TEXT)
I want the user to input a code in an entry called PCode
it looks like this
PCode = Entry(root, width=40)
PCode.grid(row=0,column=0)
then a function compares the input with all codes in the main table and takes that one and gets the table that has the same product_code.
So the sequence is. All the product tables for all product_Codes in the main table will be created and will have all data from main table that has same product_code.
Then when the program is opened the user inputs a product_code
the program picks the table that has the same code and shows it to the user.
Thanks a lot and I know it's hard but I really need your help and I'm certain you can help me. Thanks.
The product table should look like
CREATE TABLE Product_x (Product_Code INTEGER,
Product_Name TEXT, --taken from main table from lines that has same product code
Entry_Date, TEXT,
Permission_Number INTEGER,
Incoming INTEGER,
Outgoing INTEGER,
Description TEXT,
Total_Quantity_In_Store INTEGER, --which is main table's incoming - outgoing
Total_Value_In_Store INTEGER --main table's total_buying_price - total_sell_price
)
Thank you for your help and hope you can figure it out because I'm really struggling with it.
From your comment:
I think I'd select some columns from main table but I don't know how I'd update the only some columns with select columns from main table where product code = PCode.get() "which is the entry box". is that possible.
Yes, it is definitely possible to present only certain rows and columns of data to the user.
However, there are many patterns (i.e. programming techniques) that you could follow for presenting data to the user, but every common, best-practice technique always separates the backend data (i.e. database) from the user interface. It is not necessary to limit presentation of data to one entire table at a time. In most cases the data should never be presented and/or exposed to the user exactly as it appears in a table. Of course sometimes the data is simple and direct enough to do that, but most applications re-format and group data in different views for proper presentation. (Here the term view is meant as a very general, abstract term for representing data in alternative ways from how it is stored. I mention specific sqlite views below.)
The entire philosophy behind modern databases is for efficient, well-designed storage that can be queried to return just what data is appropriate for each application. Much of this capability is based on the host-language data models, but sqlite directly supports features to help with this. For instance, a view can be defined to select only certain columns and rows at a time (i.e. choose certain Produce_Code values). An sqlite view is just an SQL query that is saved and can have certain properties and actions defined for it. By default, a sqlite view is read-only, but triggers can be defined to allow updates to the underlying tables via the view.
From my earlier comment: You should research data normalization. That is the key principle for designing relational databases. For instance, you should avoid duplicate data columns like Product_Name. That column should only be in the StoringTF. Calculated columns are also usually redundant and unnecessary--don't store the Total_Value_In_Store column, rather calculate it when needed by query and/or view. Having duplicate columns invites mismatched data or at least unnecessary care to make sure all columns are synced when one is updated. Instead you can just query joined tables to get related values.
Honestly, these concepts can require much study before implementing properly. By all means, go forward with developing a solution that fits your needs, but a Stack Overflow answer is no place for full tutorials which I perceive that you might need. Really your question seems more about overall design and I think my answer can get you started on the right track. Anything more specific and you'll need to ask other questions later on.

Method to insert an array or dictionary into db database in Google App Engine - Python

I am new to Python (and OOP) and working on a challenging project (and first post here!). I tried searching, but could not find anything of use, or perhaps did not know what to search for.
Here is what I want to do:
I have two tables in db (from google.appengine.ext). One is "fruits" with name of fruits and their nutrition info and the other is "user" which I want to store two columns, uid and favFruits (fruits they like, their score (5-star scale) and a comment). Problem I am having is each user (uid row) can have multiple fruits they like and comment. The favFruits will be shown on user's profile and when the link is clicked, it goes to nutrition page.
Example:
"favFruits":[
{
"fruit":"fuji apple"
"score":"4"
"comment":"Delicious. Bit tart, but very sweet"
},
{
"fruit":"orange"
"score":"5"
"comment":"I just love it!"
}
]
What would be the best method to store this in the Google Appengine Datastore? Currently I am using db.StringListProperty() with only favFruits['fruit'] as the list input, which does not include ['score'] or ['comment']. What I really want is to store 2D table inside a column (an array into a column of db) that is efficiently search-able, as soon as uid is identified.
Is Json the best approach? What about concatenating into a single string containing all three fields into a string and store the list (like [u'orange,5,I just love it!']?
If there is a better approach, please let me know! I am stuck... Any help is greatly appreciated!
Thank you
My first advise: use NDB. Using NDB you have structured and JSON properties, built-in caching and much more: https://developers.google.com/appengine/docs/python/ndb/
You can use an NDB structured repeated property for favFruits.
https://developers.google.com/appengine/docs/python/ndb/properties#structured
A JSON property (a blob) cannot be used in a query.

Python: Dumping Database Data with Peewee

Background
I am looking for a way to dump the results of MySQL queries made with Python & Peewee to an excel file, including database column headers. I'd like the exported content to be laid out in a near-identical order to the columns in the database. Furthermore, I'd like a way for this to work across multiple similar databases that may have slightly differing fields. To clarify, one database may have a user table containing "User, PasswordHash, DOB, [...]", while another has "User, PasswordHash, Name, DOB, [...]".
The Problem
My primary problem is getting the column headers out in an ordered fashion. All attempts thus far have resulted in unordered results, and all of which are less then elegant.
Second, my methodology thus far has resulted in code which I'd (personally) hate to maintain, which I know is a bad sign.
Work so far
At present, I have used Peewee's pwiz.py script to generate the models for each of the preexisting database tables in the target databases, then went and entered all primary and foreign keys. The relations are setup, and some brief tests showed they're associating properly.
Code: I've managed to get the column headers out using something similar to:
for i, column in enumerate(User._meta.get_field_names()):
ws.cell(row=0,column=i).value = column
As mentioned, this is unordered. Also, doing it this way forces me to do something along the lines of
getattr(some_object, title)
to dynamically populate the fields accordingly.
Thoughts and Possible Solutions
Manually write out the order that I want stuff in an array, and use that for looping through and populating data. The pros of this is very strict/granular control. The cons are that I'd need to specify this for every database.
Create (whether manually or via a method) a hash of fields with an associated weighted value for all possibly encountered fields, then write a method for sorting "_meta.get_field_names()" according to weight. The cons of this is that the columns may not be 100% in the right order, such as Name coming before DOB in one DB, while after it in another.
Feel free to tell me I'm doing it all wrong or suggest completely different ways of doing this, I'm all ears. I'm very much new to Python and Peewee (ORMs in general, actually). I could switch back to Perl and do the database querying via DBI with little to no hassle. However, it's libraries for excel would cause me as many problems, and I'd like to take this as a time to expand my knowledge.
There is a method on the model meta you can use:
for field in User._meta.get_sorted_fields():
print field.name
This will print the field names in the order they are declared on the model.

How to join two data models for google app engine in python

I am trying/lerning to make application in python that will have information about universitys and their departments.
The problem I have is that I want to use data models, (I know very little about them).
I want to have two databases. One will contain departments, while other will contain universitys and list of departments.
Thanks for help!!!
Thanks for fast answers. I found some tutorials like:
http://www.youtube.com/watch?v=rRCx9e38yr8&list=PLDA31F43DE4107B05
http://blog.notdot.net/2010/10/Modeling-relationships-in-App-Engine
and I started reading them, and some thing are having more sense.
In case you are referring to the structure of your database you might want to read more about E-R Diagrams and Relational Database. I can brief you with the relational database model which might be useful in your case.
At minimum I think it can have three tables in the same database. One for the Department ids mapping to the universities. Another one mapping the departments with course names and unique ids to the departments which offer that course. And the third one mapping the unique student ids with the course ids. Then you can have additional tables mapping from student information to student id. And professor ids with professor info, professor ids with course ids which they would be teaching....and this can go on based on the data you want to store.
Also keep in mind in case you want to create foreign keys for the ids you should use InnoDB Engine as MyISAM doesnot allow foreign keys.

Categories

Resources