LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2021, 06:46 AM   #1
shams
Member
 
Registered: Jan 2004
Posts: 535

Rep: Reputation: 30
Write python mariadb qurey in lines?


This is python code, query the table pop form mariadb and writing the three columns of each row to a file in one line with comma separated:
Code:
cur  = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
        with open(f'mail{idx}.txt', 'w') as f:
                f.write(','.join(row))
I want to write the each row to file in three lines like this:
Code:
User= user
pssword= passwd
server= server

Last edited by shams; 02-03-2021 at 08:22 AM.
 
Old 02-03-2021, 09:10 PM   #2
Syndacate
Member
 
Registered: Aug 2008
Location: Santa Clara, CA
Distribution: Ubuntu, mainly. Too much stuff works out of the box O.o
Posts: 71

Rep: Reputation: 52
Quote:
Originally Posted by shams View Post
This is python code, query the table pop form mariadb and writing the three columns of each row to a file in one line with comma separated:
Code:
cur  = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
        with open(f'mail{idx}.txt', 'w') as f:
                f.write(','.join(row))
I want to write the each row to file in three lines like this:
Code:
User= user
pssword= passwd
server= server
I'm not familiar with MariaDB, but why not just wrap the file write around the db access and write each iteration? That is to say:
Code:
with open(f'mail{idx}.txt', 'w') as f:
        for idx, row in enumerate(cur, start=1):
                f.write(row + '\n')
EDIT:
I don't think this is what you want, but your post is a little confusing. If you want 3 separate text files, each containing a comma separated list, looks like you have it.
If you want one text file where each row of the file contains the row of the db, then that's what I put above. I don't understand how the bottom piece correlates to what you're asking.

Maybe you meant you want a separate file for each db row where each row in the file is a col of the db like you showed at the bottom? If that's the case. But I have no idea what this has to do with the comma separated list you mentioned:
Code:
for idx, row in enumerate(cur, start=1):
        with open(f'mail{idx}.txt', 'w') as f:
                f.write('User={0}\nPassword={1}\nServer={2}\n'.format(row[0], row[1], row[2]))
                # Alternatively argument to write can be: f'User={row[0]}\nPassword={row[1]}\nServer={row[2]}\n'  - but I prefer the former, just preference.

Last edited by Syndacate; 02-03-2021 at 09:26 PM.
 
Old 02-03-2021, 09:19 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Code:
for idx, row in enumerate(cur, start=1):
        with open(f'mail{idx}.txt', 'w') as f:
                f.write(f'User= {row[0]}\n')
                f.write(f'pssword= {row[1]}\n')
                f.write(f'server= {row[2]}\n')
 
Old 02-03-2021, 09:27 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Code:
cur  = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
  with open(f'mail{idx}.txt', 'w') as f:
    f.write('User= {}\npssword= {}\nserver= {}\n'.format(*row))
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: KDE Participating in Google Summer of Code 2019, MariaDB Releasing New Open-Source MariaDB Enterprise Server, CentOS Celebrates 15th B LXer Syndicated Linux News 0 02-28-2019 06:50 AM
Does Slackware come with Mariadb-python? Altiris Slackware 5 01-06-2016 09:41 PM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration