2018.5.8 python操縱sqlite資料庫

來源:互聯網
上載者:User

標籤:app   面向   ini   com   ocs   commit   import   one   self   

建立:

create_Email = "CREATE TABLE if not exists emails (\n\
id INTEGER NOT NULL,\n\
user VARCHAR(64),\n\
email VARCHAR(64),\n\
PRIMARY KEY (id)\n\
);"

 

try:

    sqlite_conn=sqlite3.connect(DB_SQLITE_NAME)

    sqlite_cursor.execute(create_Email)
    sqlite_conn.commit()

except sqlite3.Error,e:
    traceback.print_exc()
    sqlite_conn.rollback()
finally:
    sqlite_conn.close()

物件導向:
class Email(object):
    """docstring for Email"""
    def __init__(self):
        self.emails = []
        lines=self._get()
        if lines != None and lines != ‘failed‘:
            for line in lines:
            email = {}
            email[‘id‘] = line[0]
            email[‘user‘] = line[1]
            email[‘email‘] = line[2]
            self.emails.append(email)

    def _list_all_member(self):
        tmp_str=‘‘
        for name,value in vars(self).items():
            if value != None:
                if type(value) == int:
                    tmp_str=tmp_str+name+‘=‘+str(value)+‘,‘
                else:
                    tmp_str=tmp_str+name+‘=‘+"‘"+str(value)+"‘"+‘,‘
        return tmp_str

    def _list_all_member_insert(self,email):
        tmp_str=‘‘
        value_str=‘ VALUES (‘
        for name,value in email.items():
            if value != None:
                tmp_str+=name+‘,‘
                if type(value)==int:
                    value_str+=str(value)+‘,‘
                else:
                    value_str+="‘"+str(value)+"‘"+‘,‘
                tmp_str=tmp_str.strip(‘,‘)+‘)‘
                value_str=value_str.strip(‘,‘)+‘)‘
        return tmp_str,value_str

    def _get(self):
        try:
            rows = []
            sqlite_conn=sqlite3.connect(DB_SQLITE_NAME)
            sqlite_cursor=sqlite_conn.cursor()
            sql_select="SELECT * FROM EMAILS;"
            sqlite_cursor.execute(sql_select)
            for row in sqlite_cursor:
                rows.append(row)
            return rows
        except sqlite3.Error,e:
            return ‘failed‘
        finally:
            sqlite_conn.close()

    def delete(self,id):
        for email in self.emails:
            if email[‘id‘] == id:
                self.emails.remove(email)
                sql_delete= "DELETE FROM EMAILS WHERE id = ‘" + id + "‘;"
                local_sql_exe(sql_delete)

    def add(self,email):
        self.emails.append(email)
        sql_insert=‘INSERT INTO EMAILS ( ‘
                name_str,value_str=self._list_all_member_insert(email)
        sql_insert+=name_str+value_str
        local_sql_exe(sql_insert)

    def clear(self):
        self.emails = []
        sql_delete= "DELETE FROM EMAILS;"
        local_sql_exe(sql_delete)

調用:

from sqlite import Email

email= Email()

email.emails

email.add(email)

email.delete(id)

2018.5.8 python操縱sqlite資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.