简体   繁体   中英

Pymongo, TypeError : expected a character buffer object

I'm trying to connect and to read data in a MongoDB database, to learn Python. I'm using Pymongo and I have this error :

Traceback (most recent call last):
File "secondtest.py", line 107, in <module>
user_info = dbco.find({})
TypeError: expected a character buffer object

This is my database.ini :

[postgresql]
host=monhostname
database=monpass
port=15000
user=monuser
password=monpass

[mongodb]
hostname=127.0.0.1
database=Mydatabase
username=Myname
password=Myn@me!
collection=measure
port=27017

And my code using it :

# -*- coding: utf-8 -*-
# secondtest.py

import psycopg2
import sys
import pymongo
from urllib import quote_plus
from pymongo import MongoClient
from configparser import ConfigParser

# Connection information in database.ini
params = ''
mongollection = ''

# Variables to connect to a database, to use a cursor object and to fetch all results from a query
mongoClient = ''
pgsqlClient = ''
pgsqlCursor = ''
pgsqlRecords = ''
mongoRecords = ''
dbco = ''

# Retrieve connection information from ini file
def dbConfig(section, filename='database.ini'):
    # Create a parser
    parser = ConfigParser()

    # Read config file
    parser.read(filename)

    # Get section, depending on the database engine
    db = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            db[param[0]] = param[1]
    else:
        raise Exception('Section {0} not found in the {1} file'.format(section, filename))

    # Return data or directly as a string
    if section == 'postgresql':
        return db
    elif section == 'mongodb':
        host = ''
        username = ''
        passwd = ''
        port = ''
        dbmongo = ''
        connectstring = ''

        # Make a string to connect to MongoDB
        for key, value in db.iteritems():
            if key == 'hostname':
                host = value.encode("utf-8")
            elif key == 'username':
               username = value
            elif key == 'password':
               passwd = value
            elif key == 'database':
               dbmongo = value
            elif key == 'collection':
               mongollection = value
            elif key == 'port':
               port = value

        connectstring = "mongodb://" + username + ":" + quote_plus(passwd) + "@" + host + ":" + port
        print("Internal test = " + connectstring)
        return connectstring.encode('iso-8859-1')

# Connection to MongoDB
def connectToMongoDb():
    # The f-string is only available in Python >= 3.6
    params = dbConfig('mongodb')
    print("Parameters : " + params)
    mongoClient = MongoClient(params)

    try:
        # print("Connection to database")
        dbco = mongoClient.mongollection
        print("Here")
        print("Test dbco : " + dbco)
        print("Connected to MongoDB !")
        return dbco
    except:
        return "Error : can't connect to MongoDB !"

# Close MongoDB connection
def closeMongoDbConnection():
    # try:
    mongoClient.close()
    return 'Connection closed'
    # except:
        # return "Can't close the connection. See if you already had one or if you didn't mispell its name."

# Make a query in MongoDB
def mongoDbQuery():
    #mongocursor = mongoClient.mongollection.find()
    #for document in cursor:
        #print(document)
    mongoClient.database_names()

if __name__ == '__main__':
    dataconnect =  connectToMongoDb()
    print("Connection\n")
    #mongoDbQuery()
    #collec = mongoClient.measure
    user_info = dbco.find({})
    print(user_info)
    print(closeMongoDbConnection())

Could you help me with this problem ? I think quote_plus() or even dbco = mongoClient.mongollection is what makes this error occurs. But I'm not 100% sure and I don't see, even with the documentation, how could I resolve this.

Thank you.

I'm reading your code. I see in the beginning of the program, you create the dbco variable to point to an empty string:

dbco = ''

following after that, I see you define a couple functions, then you call find() on that string:

user_info = dbco.find({})

You're passing {} (empty dict) as parameter to the method... but as you can see in the documentation here , this method needs another string as first parameter. That causes the error you see.

Now I'm not entirely sure how to fix it because I don't know what you mean. Maybe you mean to use the dataconnect variable, since that is the one that gets the results of the connectToMongoDb function:

dataconnect =  connectToMongoDb()

I made it again and changed some little things. Now, it works. Here is the code, for people who'd need it in the future.

import sys
import pymongo
from urllib import quote_plus
from pymongo import MongoClient
from configparser import ConfigParser

client = MongoClient()
connected = ''

# Retrieve connection information from ini file
def dbConfig(section, filename='database.ini'):
    # Keep result in global variable when the function is finished
    global client

    # Create a parser
    parser = ConfigParser()

    # Read config file
    parser.read(filename)

    # Get section, depending on the database engine
    db = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            db[param[0]] = param[1]
    else:
        raise Exception('Section {0} not found in the {1} file'.format(section, filename))

    # Return data or directly as a string
    if section == 'postgresql':
        return db
    elif section == 'mongodb':
        # Variables for the connection
        host = ''
        username = ''
        passwd = ''
        port = ''
        connectstring = ''

        # Make a string to connect to MongoDB
        for key, value in db.iteritems():
            if key == 'hostname':
                host = value.encode("utf-8")
            elif key == 'username':
               username = value
            elif key == 'password':
               passwd = value
            elif key == 'database':
               dbmongo = value
            elif key == 'collection':
               mongollection = value
            elif key == 'port':
               port = value

        # Make the URI needed for the connection to Mongo DB
        passwing = "mongodb://" + username + ":" + quote_plus(passwd) + "@" + host + ":" + port
        client = MongoClient(passwing)
        return client

# Close MongoDB connection
def closeMongoDbConnection():
    # Try to close the connection to Mongo DB
    try:
        client.close()
        return 'Connection closed'
    except:
        return "Can't close the connection. See if you already had one or if you didn't mispell its name."

# Connection to MongoDB
def connectToMongoDb(mydb):
    db = client.get_database(mydb)
    return db.measure

# Make a query in MongoDB
def mongoDbQuery():
    docs = connected.find().count()
    #for document in docs:
        #print(document)
    print(docs)

if __name__ == '__main__':
    connected = connectToMongoDb('neocampus')
    #docs = connected.find()
    # print(test)
    #for document in docs:
        #print(document)
    mongoDbQuery()

    # Show if the connection to Mongo DB is a success or not
    print(closeMongoDbConnection())

The problems were : - about global variables in and out of functions - the database variable empty (because of that) - a first call of MongoClient()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM