android - 使用postgresql在android studio(kotlin)中插入记录

标签 android postgresql kotlin

是否有关于如何将数据从 PostgreSQL 插入或检索到 android studio 的文档,我正在使用 kotlin。我是 Android Studio 的新手。我的 MainActivity.ktconnectiondb.java

中已有此代码

connectiondb.java

public class connectionDb {
    Connection connection=null;
    public Connection ConnectionDb(){
        try{
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mytransactiondb", "postgres", "root");

        } catch (Exception err) {
            System.err.println(err.getMessage());
        }

        return connection;
    }
    protected void close_connection(Connection con)throws Exception{
        con.close();
    }
}

主要 Activity .kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_login.setOnClickListener{
            val objConnectionDb=connectionDb()
            objConnectionDb.connection

            Log.i("connectinDb()", "Connected")
        }
    }
}

提前致谢

最佳答案

尽管我没有在 Android 上使用 PostreSQL,但我得到了这个文档,上面写着。

下面是使用 PostreSQL for Android 插入查询的方法

PreparedStatement st=conn.prepareStatement(”INSERT INTO films (title) ”+”VALUES(?) ;”);
st.setString(1,”On Stranger Tides”);
int rows=st.executeUpdate();
st.close();

通过this link了解更多详情。

关于 here 的更多见解

关于android - 使用postgresql在android studio(kotlin)中插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62256940/

相关文章:

java - 为什么 Kotlin 类默认是 final 而不是 open?

android - 使用 Kotlin for Android 进行数据绑定(bind)的问题

android - 如何拍摄surfaceview的快照?

android - 如何在MPandroidchart库中使用图标作为饼图的标签

java - Android 数据绑定(bind)单选组

PostgreSQL pg_dump 创建sql脚本,但它不是sql脚本: is there a way to get pg_dump to create a standard sql script?

适用于 Android ADB 的 Java 库

database - 如何实现系统以确定是否已达到里程碑

postgresql - 使用 'value too long' 的非 ascii 字符移动 PostgreSQL 数据库失败

android - Kotlin 等价于 Java 的 equalsIgnoreCase