Python Forum
How to format a datetime MySQL database field to local using strftime() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: How to format a datetime MySQL database field to local using strftime() (/thread-16347.html)



How to make bottle-pymysql work - nikos - Feb-24-2019

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'



RE: How to make bottle-pymysql work - buran - Feb-24-2019

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


RE: How to make bottle-pymysql work - nikos - Feb-24-2019

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?


RE: How to make bottle-pymysql work - buran - Feb-24-2019

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)


RE: How to make bottle-pymysql work - nikos - Feb-24-2019

'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.


RE: How to make bottle-pymysql work - nikos - Feb-24-2019

Hello?! Can you help please?


How to format a datetime MySQL database field to local using strftime() - nikos - Feb-24-2019

# 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.