Click here to Skip to main content
15,747,189 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
import socket
#get IP from computer
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("", 80))
dataIP = (s.getsockname()[0])
#set connection with sql
mydb = mysql.connector.connect(
      host= "localhost",
      user = "root",
      passwd ="",
mycursor = mydb.cursor()
sql = "insert into tbluser (txtIP) values('"+dataIP+"')"
if dataIP == None:
    print("on connection")
    print("have connection")

What I have tried:

i try on this code to insert but no insert to database and have check database set to online txtIP .
Updated 3-Jul-19 4:00am
Dominic Burford 3-Jul-19 3:28am    
Are you getting an error? Do you have error logs you can check? Do you have a valid connection? Is your INSERT statement correct? I have no idea what has failed without you providing more detail.
Member 11271238 3-Jul-19 5:11am    
i forget with mydb.commint() i see my problem, thank you

1 solution

As you mentioned in the comments, you have fixed this by using mydb.commit(). Adding this solution so that this question no longer shows in the Unanswered queue.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900