Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attendence System
#11
You should post some more code. It looks like you haven't connected to the database yet.
Reply
#12
Ok didnt know how much i should post. The Mysql server is on the same machine i am using to write the code.

import time


import mysql.connector


from mysql.connector import errorcode


from datetime import datetime


try:
   cnx = mysql.connector.connect(user='root',database='staff', host='localhost')
   
except mysql.connector.Error as err:
   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
       print("Something is wrong with your username or password")
   elif err.errno == errorcode.ER_BAD_DB_ERROR:
       print("Database does not exist")
   else:
       print(err)
else:
   cnx.close()

cursor = cnx.cursor()
Thats when i receive the error.
Error:
--------------------------------------------------------------------------- OperationalError                          Traceback (most recent call last) <ipython-input-6-23c84600e42c> in <module>() ----> 1 cursor = cnx.cursor() /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in cursor(self, buffered, raw, prepared, cursor_class, dictionary, named_tuple)   1381             raise errors.InternalError("Unread result found.")   1382         if not self.is_connected(): -> 1383             raise errors.OperationalError("MySQL Connection not available.")   1384         if cursor_class is not None:   1385             if not issubclass(cursor_class, CursorBase): OperationalError: MySQL Connection not available.
Reply
#13
Do you have privileges to connect to the db?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
(Dec-20-2016, 06:30 PM)wavic Wrote: Do you have privileges to connect to the db?

I think i do how would i check from the mysql console? + shouldnt it give me a error if i couldn't on the line above?
Reply
#15
Try pinging the server after you've got a connection, but before trying to make a cursor. That way you can make sure the database is running and you're connected. https://dev.mysql.com/doc/connector-pyth...-ping.html

You could also skip the cursor and use the connection directly with .cmd_query(): https://dev.mysql.com/doc/connector-pyth...query.html
Reply
#16
(Dec-20-2016, 06:41 PM)nilamo Wrote: Try pinging the server after you've got a connection, but before trying to make a cursor.  That way you can make sure the database is running and you're connected.  https://dev.mysql.com/doc/connector-pyth...-ping.html  

You could also skip the cursor and use the connection directly with .cmd_query(): https://dev.mysql.com/doc/connector-pyth...query.html

ok so i tried to ping the server and i still gives me a big error. NEW code
import time


import mysql.connector


from mysql.connector import errorcode


from datetime import datetime


try:
   cnx = mysql.connector.connect(user='root', password='password', database='staff', host='localhost')
   
except mysql.connector.Error as err:
   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
       print("Something is wrong with your username or password")
   elif err.errno == errorcode.ER_BAD_DB_ERROR:
       print("Database does not exist")
   else:
       print(err)
else:
   cnx.close()


cnx.ping(attempts=1,delay=0)
Then i get the error
Error:
OSError                                   Traceback (most recent call last) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/network.py in send_plain(self, buf, packet_number)    129                 else: --> 130                     self.sock.sendall(packet)    131             except IOError as err: OSError: [Errno 9] Bad file descriptor During handling of the above exception, another exception occurred: OperationalError                          Traceback (most recent call last) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in ping(self, reconnect, attempts, delay)   1011         try: -> 1012             self.cmd_ping()   1013         except: /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in cmd_ping(self)    874         """ --> 875         return self._handle_ok(self._send_cmd(ServerCmd.PING))    876 /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in _send_cmd(self, command, argument, packet_number, packet, expect_response)    503                 self._protocol.make_command(command, packet or argument), --> 504                 packet_number)    505         except AttributeError: /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/network.py in send_plain(self, buf, packet_number)    132                 raise errors.OperationalError( --> 133                     errno=2055, values=(self.get_address(), _strioerror(err)))    134             except AttributeError: OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 9 Bad file descriptor During handling of the above exception, another exception occurred: InterfaceError                            Traceback (most recent call last) <ipython-input-6-ee7505b13fb4> in <module>() ----> 1 cnx.ping(attempts=1,delay=0) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in ping(self, reconnect, attempts, delay)   1015                 self.reconnect(attempts=attempts, delay=delay)   1016             else: -> 1017                 raise errors.InterfaceError("Connection to MySQL is"   1018                                             " not available.")   1019 InterfaceError: Connection to MySQL is not available.
Sorry for the big errors and for me being not very good at python. :) I have learned loads over the last few days and i thank you all for your help.
Reply
#17
Looks to me like the server is not running
Reply
#18
(Dec-21-2016, 01:36 PM)Larz60+ Wrote: Looks to me like the server is not running

I can access the server from the console though. i start the server from the terminal and then i can access it. I presume that means it is up?
Reply
#19
Ok problem solved. I started up a apache server on the same PC and it seemed to fix it. Don't really
Understand but it now works. Thanks very much.
Reply
#20
Ok so thought I would use the same thread even though I am much further on. Now I am looking at the clocking out section. I have managed to get the system to get them to input their staff id twice. it then goes to check the server. if it finds the right code in one of the MYSQL tables it returns saying clocked it. It then adds a row to a different table with the date in one column, the "time in" in another and their name in the last. 

How do I get it to check if they are already clocked in that day and if they are, clock them out by adding a time into the clock out column.

Any help would be great thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,604 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 6,799 Dec-02-2019, 04:41 PM
Last Post: Agile741

Forum Jump:

User Panel Messages

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