-1

I am working on a user register and login API. DB is MySQL. getAllUser is getting Properly. But when i am trying for post method for Inserting a row it's keep throwing this Error.

TypeError: not all arguments converted during string formatting

MY CODE: DB structure:

 id(autoincrement), usr_name(varchar 30), password(varchar 20)

CODE for inserting :

As I am getting all user list perfectly the connections are correct.

mysql = MySQL()

app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'xxx'
app.config['MYSQL_DATABASE_PASSWORD'] = 'xxx'
app.config['MYSQL_DATABASE_DB'] = 'xxx'
app.config['MYSQL_DATABASE_HOST'] = 'xxx.xxx.xxx.xxx'

mysql.init_app(app)


class UserRegister(Resource):
    TABLE_NAME = 'user'

    parser = reqparse.RequestParser()
    parser.add_argument('usr_name',
        required=True,
        help="This field cannot be left blank!"
    )
    parser.add_argument('password',
        required=True,
        help="This field cannot be left blank!"
    )


    def post(self):
        data = UserRegister.parser.parse_args()
        print(data)
        cursor = mysql.connect().cursor()

        query = "INSERT INTO {table} VALUES (NULL, ?, ?)".format(table=self.TABLE_NAME)
        cursor.execute(query, (data['usr_name'], data['password']))

        return {"message": "User created successfully."}, 201

Please help.. I really need this .. or any tutorial to follow on.

1
  • Replace query by "INSERT INTO {table} (usr_name, password) VALUES (?, ?)".format(table=self.TABLE_NAME) Commented Mar 3, 2018 at 6:35

1 Answer 1

0

ERROR SOLVED

def post(self):
            data = UserRegister.parser.parse_args()
            print(data)

        cursor = mysql.connect().cursor()

        username = request.json['usr_name']
        password = request.json['password']

        query = "insert into user values(null,'{0}','{1}')".format(username,password)
        cursor.execute(query)

        return {"message": "User created successfully."}, 201
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.