2

my code is,

import MySQLdb
import collections
from pymongo import Connection

from config.MySQLdb import *
from config.MongoDB import *
from config.SyncTables import *

db = MySQLdb.connect(DB_HOST,DB_USR,DB_PWD,DB_NAME)
cursor = db.cursor()
query = "SELECT * FROM table_name WHERE userid = 1"
cursor.execute(query)
row = cursor.fetchall()

mconn = Connection(MONGODB_HOST,MONGODB_PORT)
mdb = mconn[MONGODB_DBNAME]
#col = mdb[trngl_advertiser_agegroup]

db.trngl_advertiser_agegroup .insert({'id': row[0],'userid':row[1], 'ageid':row[2],       'age_value':row[3], \                                             'created':row[4],  'modified':row[5]})
print "after update"

but I'm getting this error,

[email protected] [~/download/DataInsertionScript]# python IngestDataToMongo.py

  File "IngestDataToMongo.py", line 21
    db.table_name.insert({'id': row[0],'userid':row[1], 'ageid':row[2],  'age_value':row[3],\                                                       'created':row[4], 'modified':row[5]})
                                                                                                                                                                                               ^

SyntaxError: unexpected character after line continuation character

But I don't see any unexpected character after . So please tell me, why I'm getting this error

The error image is, enter image description here

2

2 Answers 2

6

Your code is all on one line, but you are using a line continuation escape character (\). Remove that character:

db.trngl_advertiser_agegroup .insert({'id': row[0],'userid':row[1], 'ageid':row[2], 'age_value':row[3], 'created':row[4],  'modified':row[5]})

You do not need to use such a line continuation trick at all when you break the line inside parenthesis or brackets, you can safely put the statement on multiple lines without it:

db.trngl_advertiser_agegroup .insert({
    'id': row[0],
    'userid':row[1], 
    'ageid':row[2], 
    'age_value':row[3],
    'created':row[4],
    'modified':row[5]
})
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, it worked after removing that character. But I'm really frustrated with python and mongodb connection. Yesterday, it was giving me error when I didn't use \, and today my some code is on next line, and no need to use \. Now I'm thinking why it is showing me my code split-ed in 2 lines. I'm seeing code using cPanelX in browser
Your line was wrapped by the terminal or editor, there isn't an actual newline there, just a hell of a lot of extra whitespace that is redundant.
1

You don't need line continuation characters inside [], {}, or (). Feel free to put the arguments in multiple lines if you want without \

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.