Fetching and writing source code from and to DB records



  • Hello,

    I have some questions concerning a special plugin.
    Maybe someone else has already developed a plugin similar to my project.

    My source code is stored within a DB and NOT in a simple file. I want to read and
    write source code directly from to the DB.

    DB-querying is not a problem.

    But, how to insert query results into the editor (e.g. from the clipboard).
    Where to find an … how to get started.

    Has anyone got an idea.

    Thanks a lot, beginner.



  • I have some questions concerning a special plugin.
    Maybe someone else has already developed a plugin similar to my project.

    My source code is stored within a DB and NOT in a simple file. I want to read and
    write source code directly from to the DB.

    DB-querying is not a problem.

    But, how to insert query results into the editor (e.g. from the clipboard).
    Where to find an … how to get started.

    Has anyone got an idea.

    Thanks a lot, beginner.

    Hello Michael-Gollnick,

    well I don’t have a specialized plugin for this but maybe
    you can use the python script plugin to achieve this.
    Which database do you use?
    If it is SQLite than it is easy because it is already
    part of python 2.7, if it isn’t than maybe someone
    has already written a py module to access it.

    So two scripts like those could myabe solve your problem

    <code>
    import sqlite3

    conn = sqlite3.connect(r’D:\my_database.sqlite’)
    c = conn.cursor()
    c.execute(‘SELECT Code_ID, Code FROM Code’)
    records = c.fetchall()
    dict_record = dict(records)
    msg = “”
    for record in records:
    id, code = record
    msg += “{0} {1}\n”.format(id, code)

    console.write(’ Codes \n’ + msg)

    id = notepad.prompt(“Select the ID from the console output window”, ‘SELECT Code_ID, Code FROM Code’, ‘nothing chosen’)

    if id != ‘nothing chosen’:
    notepad.new(id)
    editor.addText(dict_record[int(id)])

    c.close()
    <code>

    Cheers
    Claudia



  • @Michael-Gollnick

    2nd try - both example scripts

    I have some questions concerning a special plugin.
    Maybe someone else has already developed a plugin similar to my project.

    My source code is stored within a DB and NOT in a simple file. I want to read and
    write source code directly from to the DB.

    DB-querying is not a problem.

    But, how to insert query results into the editor (e.g. from the clipboard).
    Where to find an … how to get started.

    Has anyone got an idea.

    Thanks a lot, beginner.

    Hello Michael-Gollnick,

    well I don’t have a specialized plugin for this but maybe
    you can use the python script plugin to achieve this.
    Which database do you use?
    If it is SQLite than it is easy because it is already
    part of python 2.7, if it isn’t than maybe someone
    has already written a py module to access it.

    So two scripts like the followings could maybe solve your problem

    [code]
    import sqlite3

    conn = sqlite3.connect(r’D:\my_database.sqlite’)
    c = conn.cursor()
    c.execute(‘SELECT Code_ID, Code FROM Code’)
    records = c.fetchall()
    dict_record = dict(records)
    msg = “”
    for record in records:
    id, code = record
    msg += “{0} {1}\n”.format(id, code)

    console.write(’ Codes \n’ + msg)

    id = notepad.prompt(“Select the ID from the console output window”, ‘SELECT Code_ID, Code FROM Code’, ‘nothing chosen’)

    if id != ‘nothing chosen’:
    notepad.new(id)
    editor.addText(dict_record[int(id)])

    c.close()
    [/code]

    and second script to save the changes

    [code]
    import sqlite3, os

    conn = sqlite3.connect(r’D:\my_database.sqlite’)
    c = conn.cursor()
    c.execute(r’UPDATE Code SET Code = ? WHERE Code_ID = ? ',(editor.getText(),os.path.basename(notepad.getCurrentFilename())))
    if c.rowcount > 0:
    conn.commit()
    os.remove(notepad.getCurrentFilename())
    console.write(“CODE UPDATED”)
    else:
    console.writeError(“UPDATE failed”)
    c.close()
    [/code]

    For easy access you can assign icons (16x16 bmp) to the scripts and add it to the notepad toolbar.

    Cheers
    Claudia


Log in to reply