473,608 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connection not defined Error in the program

1 New Member
Dear All,
I have a problem while data logging in a single table in SQL DB. We are not able to do it. When we are running the python code in raspberry pi we are getting the following error msg.

Program:
Expand|Select|Wrap|Line Numbers
  1. import serial
  2. import time
  3. import MySQLdb as mdb
  4.  
  5. arduino = serial.Serial("/dev/ttyACM0")
  6. arduino.baudrate=9600
  7.  
  8. data = arduino.readline()
  9. time.sleep(1)
  10. data = arduino.readline()
  11. pieces = data.split("\t")
  12.  
  13. temperature = pieces[0]
  14. humidity = pieces[1]
  15.  
  16. con = mdb.connect('localhost', 'root', '12345678', 'testdb');
  17.  
  18. with con:
  19.  
  20.     cursor = connection.cursor(prepared=True)
  21.     sql_insert_query = """ INSERT INTO TempDB (temperature, humidity) VALUES (?,?)"""
  22.  
  23.     insert_tuple = (temperature, humidity)
  24.     cursor.execute(sql_insert_query, insert_tuple)
  25.     connection.commit()
Error Message

Traceback (most recent call last):
File "./insertDB.py", line 20, in <module>
cursor = connection.curs or(prepared=Tru e)
NameError: name 'connection' is not defined
Jun 23 '20 #1
4 22045
Banfa
9,065 Recognized Expert Moderator Expert
I'm not a Python expert but it looks to me like you added the with con: and forget to remove the connection. at lines 20 and 25.
Jun 23 '20 #2
AjayGohil
83 New Member
Hi,

You use con variable to store connection string and and use connection variable. so,use con instead of connection.
Jun 24 '20 #3
shkum
1 New Member
I got same error when tried to connect to DB wich was not yet created
Aug 24 '22 #4
sandrahdes
1 New Member
When we try to access a variable or function that is not defined or before it is defined, Python throws the error "NameError: name is not defined." Make sure the variable's name is spelled correctly and access it after it has been defined to fix the issue. Here is an illustration of the fallacy in action. When a variable, function, or module is used in Python but either doesn't exist or wasn't used properly, a NameError is raised. Common errors that lead to this error include the following: using a name for a variable or function that hasn't yet been declared.
Sep 2 '22 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4739
by: beav At wn DOT com DOT au | last post by:
Students at the school where I work are getting the error "Application-defined or object-defined error." when trying to document their Access databases. If you open the documenter window, select a table and click OK, it works. If you click options and then click OK the above error message comes up. When logged in as administrator, the error does not happen. I assume that it is therefore a permissions problem. I have used google, and...
6
2339
by: Chris Szabo | last post by:
I've created a data access layer for a .NET web application. I'm using C# and framework 1.1. I'm getting an error from time to time when I close a connection saying: System.InvalidOperationException: A connection pooling error has occurred. It doesn't happen each time, the exception is thrown randomly. The big problem is that the connection is left active in the pool. After a short period of time, the pool is filled up and people...
0
1430
by: May | last post by:
Hello, We got this error (Internal connection fatal error) from our application and at the same we got "server application unavailable" error on the browser. I looked at the line that was provided by the stack trace and it appeared that it happened when it tries to connect to the database to fill dataset. What might cause the database connection to fail or cause this error? System.Data
5
12825
by: Henry | last post by:
I am writing a Windows forms VB.Net/MS SQL application that utilizes Crystal Reports. I want to be able to dynamically set the report data source at run time. According to article "cr_rdc9_connectionproperties.pdf" on the Crystal support web site I should be able to do the following: Dim ConnectionInfo As CRAXDRT.ConnectionProperties I get a "Type CRAXDRT.connectionProperties is not defined" error from the VS designer when I enter the...
1
17566
by: Sylaris | last post by:
hi, i have a problem which recursion fits perfectly, however; after working with the base function (which has no errors and works correctly) the recursions return a "function not defined" error in ff's js console. i tried a few things that came to mind but nothing has changed it... i found some interesting articles on the net regarding javascript's callbacks, and found out that due to callbacks recursion may end up eating a lot more memory than...
0
1266
by: ewarts | last post by:
Hello all, I keep getting the following error whenever i execute a access program. The program is supposed to retrieve data from the source database and then put it into an excel spreadsheet then replace it into a new excel spreadsheet which is a formatted sheet. Can anyone assist. Here is the error below in bold, the second line is the line that the error is trying to debug: Application-defined or object-defined error ...
2
8809
by: LeoK | last post by:
After finally completing my database I was ready to transfer it to our client, but when the access database was opened, everything works great, except for the SEND_EMAIL part, whenever any code that has to do with opening outlook I get a error, . I panic, so I try it on another computer and the same thing happened. After further investigation I realize that on both machine outlook wasn't set up yet, meaning no account existed. My only question...
7
13447
by: JFKJr | last post by:
Hi! I am a new beginner to MS Access VBA, I wrote the following VBA code in Access which deletes blank columns and rows in excel file. But, the code works fine for sometimes and sometimes it displays "application defined or object defined error". And, when I step into the code, error is occuring at the following Bold line. Please help me with this problem. Thank you in advance. Sub RunMacro() On Error GoTo Err_RunMacro Dim XL As Object...
0
1636
by: Hema Suresh | last post by:
Hi all I am trying to create a database using vb6 and excel I created few controls on the vb form and coded to get the details from the user I code in the way to pass the data which i got from the user to an excel sheet I named a function in a module as "Appending" and i made it to receive all the data through arguments And almost I coded correctly to make the database look in the format i need Eveeything is OK,but when I am running the...
6
1371
topher23
by: topher23 | last post by:
I have an Access subroutine that needs to generate an Excel spreadsheet for accounting. I've used late-bound Excel automation before, but for some reason I keep running into a strange issue when I run this one. This is the relevant portion of the procedure: Private Sub GenerateExplodedBOM() 'On Error GoTo Err_Handler Dim appXL As Object Dim wbBOM As Object Dim rs As New adodb.Recordset
0
8067
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5479
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3967
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1336
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.