ماکروها/بیسیک/دادگان

    From The Document Foundation Wiki
    This page is a translated version of the page Macros/Basic/Base and the translation is 100% complete.
    Other languages:

    بازگشت به نمایه

    دادگان

    اتصال به پایگاه داده ثبت شده

    Sub Main()	
        con = get_db("Bibliography")
    End Sub
    
    Function get_db(db_name As String)
        dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
        If dbc.hasByName(db_name) Then
            db = dbc.getByName(db_name)
            con = db.getConnection("","")
        End If
        get_db = con
    End Function

    ایجاد جدول با اسکیوال

    con = get_db("test")
    
    sql = "CREATE TABLE ""person"" (""id"" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY NOT NULL, ""name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL)"
    cursor = con.createStatement()
    cursor.execute(sql)
    con.getParent().DatabaseDocument.store()
    con.close()

    درج داده با اسکیوال

    con = get_db("test")
    
    sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES ('Teresa', 'Salgueiro')"
    cursor = con.createStatement()
    cursor.execute(sql)
    con.getParent().DatabaseDocument.store()
    con.close()


    درج داده با استفاده از prepareStatement

    con = get_db("test")
    
    sql = "INSERT INTO ""person"" (""name"", ""last_name"") VALUES (?, ?)"
    cursor = con.prepareStatement(sql)
    cursor.setString(1, "Teresa")
    cursor.setString(2, "Salgueiro")
    cursor.execute()
    con.getParent().DatabaseDocument.store()
    con.close()