Python Forum
how do change data in a database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do change data in a database
#1
i have list of list

listofplayers = [[player1, 5,1,300,100],[player11, 10,5,650,150],[player23, 17,6,1100,1050]]

and i have a database

player, win, lost, moneywin, moneylost
player1, 3 , 0 , 250 , 50
player2 ...
player3 ...
...

how do i update my database with the new info in my listofplayers? Like this:

player, win, lost, moneywin, moneylost
player1, 5, 1, 300, 100,
player2 ...
player3 ...
player11, 10, 5, 650, 150
player23, 17, 6, 1100, 1050

i tried different things but i just can't find the resources

here's my best attempt:

for i in listofplayers
    for j, k in db.iterrows():
        if i[0] == j['player']:
            index = db.index[db[j]==k].tolist() 
        db.at[index,j] = i[y]
        index = []
    y+=1
    
as you can see i'm having troubles...
this is my very first attempt at pandas and i'm still new to programming
Reply
#2
Which DBMS?
You will need to use SQL (database query language).
Reply
#3
(Mar-18-2022, 01:21 AM)Larz60+ Wrote: Which DBMS?
You will need to use SQL (database query language).

i just load a csv file with
db = pd.read_csv('playerhistory.csv')
db.to_csv('playerhistory.csv')
Reply
#4
Data is arranged serially in a CSV file, when I think of database, I think about tables that are arranged in a DBMS such as sqlite, PostGreSQL, Oracle, MySQL, etc. Tables usually are indexed by one or more fields, and there is a query language SQL that allows access to table data in many different ways, including selecting related date from more tables, all in the same query.

You can store data in a CSV file, but to change that data, you usually read the data in sequentially, looking for the data you want to change, and then writing it back out.

Most everything that I need to store I save in json files, and that works in most instances.
If there is a lot of related data, I'll use PostGreSQL.

There are lots of tutorials available on all of the methods mentioned, and many books written on relational databases and other storage methods.

Lots to learn.
Reply
#5
(Mar-19-2022, 02:05 AM)Larz60+ Wrote: Data is arranged serially in a CSV file, when I think of database, I think about tables that are arranged in a DBMS such as sqlite, PostGreSQL, Oracle, MySQL, etc. Tables usually are indexed by one or more fields, and there is a query language SQL that allows access to table data in many different ways, including selecting related date from more tables, all in the same query.

You can store data in a CSV file, but to change that data, you usually read the data in sequentially, looking for the data you want to change, and then writing it back out.

Most everything that I need to store I save in json files, and that works in most instances.
If there is a lot of related data, I'll use PostGreSQL.

There are lots of tutorials available on all of the methods mentioned, and many books written on relational databases and other storage methods.

Lots to learn.

But i load the csv file using pandas and in the tutorials they , i think, refer to info that has been loaded into pandas as a database. Is it a bad use of pandas to load the data from a csv file, make modification to it and then save the modified data as a csv file?
Reply
#6
Quote:Is it a bad use of pandas to load the data from a csv file, make modification to it and then save the modified data as a csv file
Not at all, and technically I guess you can call any accumulation of data a database.

The formal definition is:
a structured set of data held in a computer, especially one that is accessible in various ways.
"a database covering nine million workers"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change input data set for character recognition program? plumberpy 5 1,694 Apr-20-2022, 07:46 PM
Last Post: jefsummers
  Using MySQL database images or data dumps Planetary_Assault_Systems 1 1,802 Jul-20-2021, 04:40 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020