Search code examples
pythonpywin32

Win32Clipboard.GetClipboardData - Formatting


I have the following code to read the data from the clipboard and add it to a database:

    import win32clipboard, sqlite3
try:
    win32clipboard.OpenClipboard()
    data = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    print data[0] # testing
    print data[1] # testing
    test = ''.join(data)
    print test

    conn = sqlite3.connect('DB')
    cur = conn.cursor()
    cur.execute('INSERT INTO Clipboard (Data) VALUES (?)', (test))
    conn.commit()
    conn.close()


except TypeError as e:
    pass
    print e

As an example, the clipboard data is "stackoverflow".
The following error is received:

 s
Traceback (most recent call last):
t
stackoverflow
  File "clipboard.py", line 13, in <module>
    cur.execute('INSERT INTO Clipboard (Data) VALUES (?)', (test))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 13 supplied.  

I assumed that the output of the "data" was in a tuple or list. So i tried to join the values in multiple ways, the above join only represents one of the ways that was tested.
Any help would be appreciated.


Solution

  • Parameter list must be a sequence.
    You send string - sqlite reading it as sequence of a letters.
    Just add comma (test,) to send tuple with one element or use a list [test].