Python Forum
How to format a datetime MySQL database field to local using strftime()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format a datetime MySQL database field to local using strftime()
#1
I decided to move from 'pymysql' to 'bottle-pymysql' plugin. i have this code:

# dbhost is optional, default is localhost
plugin = bottle_pymysql.Plugin( dbuser='nikos', dbpass='*******', dbname='counters', charset = 'utf8' )
app.install(plugin)

.....
.....

for visit in visits:
		visit = visit.strftime('%A %e %b,  %I:%M %p')
When trying to read that view, bottle() gives me this error:

Output:
Traceback: Traceback (most recent call last): File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle return route.call(**args) File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in wrapper rv = callback(*a, **ka) File "/usr/lib/python3.6/site-packages/bottle_pymysql.py", line 174, in wrapper rv = callback(*args, **kwargs) File "/home/nikos/wsgi/www.py", line 132, in log visit = visit.strftime('%A %e %b, %I:%M %p') AttributeError: 'str' object has no attribute 'strftime'
What does this error mean?

I don'understand it, because before using bottle_pymysql i uses pymysql adn that line was interpreted correctly. Now with bottle_pymysql it does not. What can i do?
Thank you.

and also this error:

@app.route( '/' )
@auth_basic(is_authenticated_user)
def index( pymydb ):

	pdata = ''
	names = []

	pymydb.execute( '''SELECT name, phone, hits, money FROM clients ORDER BY hits DESC''' )
	data = pymydb.fetchall()
Output:
TypeError("index() missing 1 required positional argument: 'pymydb'",) Traceback: Traceback (most recent call last): File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle return route.call(**args) File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in wrapper rv = callback(*a, **ka) File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in wrapper return func(*a, **ka) TypeError: index() missing 1 required positional argument: 'pymydb'
Reply
#2
strftime() method is used to create a str representation of date, datetime or time object. In your case visit is already a str. And str does not have strftime method
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
But that line before using 'bottle-pymysql' plugin worked without problem. Only after using bottle-pymysql iam receiving this error. How should i write it?
Reply
#4
If you didn't change anything else, I would guess both plugins return different type. You don't need to do anything if visit is in desired str format (i.e. just comment out that line)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
'visit' is being returned from database containing a MySQL datatime field that i want to change to another format whcih is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally.

How should i write it?

Please tell me how should i make the conversion, as results are appearing with error if i don't convert.
Reply
#6
Hello?! Can you help please?
Reply
#7
# dbhost is optional, default is localhost
plugin = bottle_pymysql.Plugin( dbuser='nikos', dbpass='*******', dbname='counters', charset = 'utf8' )
app.install(plugin)
 
.....
pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests
					WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page )
	data = pymydb.fetchall()
        for visit in visits:
              visit = visit.strftime('%A %e %b,  %I:%M %p')
'visit' is being returned from database containing a MySQL datatime field that i want t
o change to another format whcih is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User Input to mySQL database newbie1 3 1,806 Aug-26-2020, 10:42 AM
Last Post: newbie1
  Flask export/upload database table in cvs/xlsx format steve87bg 4 2,242 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  MySQL Database Flask maurosmartins 0 784 Oct-03-2019, 10:56 AM
Last Post: maurosmartins
  mysql database error brecht83 1 3,539 Dec-14-2018, 01:25 PM
Last Post: jeanMichelBain

Forum Jump:

User Panel Messages

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