在android上使用SQL保存数据

时间:2017-02-08 11:58:42

标签: java android xml sqlite save

public void createDatabase(){

        try {
            Class.forName("org.sqlite.JDBC").newInstance();

            Connection connection = DriverManager.getConnection("jdbc:sqlite:occDatabase.db");

            Statement statement = connection.createStatement();

            String sqlStatement = "CREATE TABLE 'Player_Data' (" +
                    "'userMoney' TEXT," +
                    "'oysters' TEXT," +
                    "'clams' TEXT," +
                    "'cockles' TEXT," +
                    "'oysterPrice' TEXT," +
                    "'clamPrice' TEXT," +
                    "'cocklePrice' TEXT," +
                    "'oysterMod' TEXT," +
                    "'clamMod' TEXT," +
                    "'cockleMod' TEXT," +
                    "'cartPrice' TEXT," +
                    "'fishStandPrice' TEXT," +
                    "'storePrice' TEXT," +
                    "'superMarketPrice' TEXT," +
                    "'cartAmount' TEXT," +
                    "'fishStandAmount' TEXT," +
                    "'storeAmount' TEXT," +
                    "'superMarketAmount' TEXT," +
                    ");";

            statement.execute(sqlStatement);
            connection.commit();
            statement.close();
            connection.close();



        } catch (Exception e){
        }
    }

    public void writeToDatabase(){
        try{

            clearData();

            createDatabase();

            Class.forName("org.sqlite.JDBC");

            Connection connection = DriverManager.getConnection("jdbc:sqlite:occDatabase.db");

            Statement statement = connection.createStatement();

            String sqlStatemnt = "INSERT INTO Player_Data(userMoney,oysters,clams,cockles,oysterPrice,clamPrice,cocklePrice,oysterMod,clamMod,cockleMod" +
                    ",cartPrice,fishStandPrice,storePrice,superMarketPrice,cartAmount,fishStandAmount,storeAmount,superMarketAmount) " +
                    "VALUES('" + MainActivity.userMoney + "', '"+ GatheringSystem.oysters +"', '" + GatheringSystem.clams +"', '" + GatheringSystem.cockles +"'," +
                    " '" + Modifiers.oysterPrice +"', '" + Modifiers.clamPrice +"', '" + Modifiers.cocklePrice  +"', '" + Modifiers.oysterMod +"', '" + Modifiers.clamMod +"', '" + Modifiers.cockleMod +
                    "', '" + MainActivity.cartPrice +"', '" + MainActivity.fishStandPrice +"', '" + MainActivity.storePrice +"', '" + MainActivity.superMarketPrice +"', '" + MainActivity.cartAmount +"'," +
                    " '" + MainActivity.fishStandAmount +"', '" + MainActivity.storeAmount +"', '" + MainActivity.superMarketAmount +"')";

            statement.execute(sqlStatemnt);
            connection.commit();
            statement.close();
            connection.close();


        }catch (Exception e){

        }
    }

    public void readFromDatabase(){

        Connection connection = null;
        Statement statement = null;

        try {

            Class.forName("org.sqlite.JDBC").newInstance();

            connection = DriverManager.getConnection("jdbc:sqlite:occDatabase.db");

            statement = connection.createStatement();

        } catch (Exception e){

        }

        try{

            ResultSet resultSet = statement.executeQuery("SELECT * FROM Player_Data");

            while (resultSet.next()){
                MainActivity.userMoney = Double.parseDouble(resultSet.getString("userMoney"));

                MainActivity.cartPrice = Double.parseDouble(resultSet.getString("cartPrice"));
                MainActivity.fishStandPrice = Double.parseDouble(resultSet.getString("fishStandPrice"));
                MainActivity.storePrice = Double.parseDouble(resultSet.getString("storePrice"));
                MainActivity.superMarketPrice = Double.parseDouble(resultSet.getString("superMarketPrice"));

                MainActivity.cartAmount = Integer.parseInt(resultSet.getString("cartAmount"));
                MainActivity.fishStandAmount = Integer.parseInt(resultSet.getString("fishStandAmount"));
                MainActivity.storeAmount = Integer.parseInt(resultSet.getString("storeAmount"));
                MainActivity.superMarketAmount = Integer.parseInt(resultSet.getString("superMarketAmount"));

                Modifiers.oysterPrice = Double.parseDouble(resultSet.getString("oysterPrice"));
                Modifiers.clamPrice = Double.parseDouble(resultSet.getString("clamPrice"));
                Modifiers.cocklePrice = Double.parseDouble(resultSet.getString("cocklePrice"));

                Modifiers.oysterMod = Double.parseDouble(resultSet.getString("oysterMod"));
                Modifiers.clamMod = Double.parseDouble(resultSet.getString("clamMod"));
                Modifiers.cockleMod = Double.parseDouble(resultSet.getString("cockleMod"));

                GatheringSystem.oysters = Double.parseDouble(resultSet.getString("oysters"));
                GatheringSystem.clams = Double.parseDouble(resultSet.getString("clams"));
                GatheringSystem.cockles = Double.parseDouble(resultSet.getString("cockles"));

            }

            resultSet.close();
            statement.close();
            connection.close();

            MainActivity.databaseStarted = true;
        } catch (Exception e){
            MainActivity.databaseStarted = false;
        }
    }

}

所以我对Android的编码很新,我正试图为我的游戏保存数据,所以当我回到游戏时我可以从文件中读取。

目前它似乎并没有创建一个数据库'一点都不。

我需要给它一定的权限吗?或者它可以自动在用户手机上创建文件吗?

private void databaseMethod(){

    database.readFromDatabase();

    if(!databaseStarted){
        database.createDatabase();
        toast = toast.makeText(getApplicationContext(),"Created Database",Toast.LENGTH_SHORT);
        toast.show();
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    distThread.setDaemon(true);
    distThread.start();
    databaseMethod();
    File file = new File(getFilesDir(), "occDatebase.db");
}

@Override
protected void onPause() {
    super.onPause();
    database.writeToDatabase();
    toast = toast.makeText(getApplicationContext(),"Game Saved",Toast.LENGTH_SHORT);
    toast.show();
}

在创建时,我检查是否已有数据库,如果没有,则应创建数据库。

但它似乎甚至没有这样做。

有什么建议吗?

感谢。

1 个答案:

答案 0 :(得分:0)

Android没有JDBC。

并且所有except / for i, sig_id in enumerate(signal_ids): if sig_id in aux_dict.keys(): aux_dict[sig_id] = signal_values[i] 块都会阻止任何错误消息。

有关如何使用Android数据库框架,请参阅documentation或任何教程。