Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package com.example.sample;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import android.app.Activity;
  9. import android.content.ContentValues;
  10. import android.content.Context;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.os.Bundle;
  14. import android.widget.TextView;
  15.  
  16. public class MainActivity extends Activity{
  17.  
  18. static final String DB = "sample.db";
  19. static final int DB_VERSION = 1;
  20. static final String CREATE_TABLE = "create table sample ( id integer primary key, name string, score integer);";
  21. static final String DROP_TABLE = "drop table sample;";
  22. static private SQLiteDatabase mydb;
  23. TextView tv1;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. tv1 = (TextView)findViewById(R.id.textView1);
  31.  
  32. int ID;
  33. String NAME;
  34. int SCORE;
  35. String[] array;
  36. String s;
  37. InputStream ip1;
  38.  
  39. MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(getApplicationContext());
  40.  
  41. try {
  42. ip1 = getResources().getAssets().open("SERVICE_DATE.csv");
  43. BufferedReader bfr1 = new BufferedReader(new InputStreamReader(ip1));
  44.  
  45. mydb = hlpr.getWritableDatabase();
  46.  
  47. while ((s = bfr1.readLine()) != null){
  48. array = s.split( "," );
  49. ID = Integer.parseInt( array[0] );
  50. NAME = array[1];
  51. SCORE = Integer.parseInt( array[2] );
  52. tv1.append(array[0] + "\t" + array[1] + "\t" + array[2] + "\n");
  53. insertData(ID,NAME,SCORE);
  54. }
  55. } catch (IOException e) {
  56. // TODO 自動生成された catch ブロック
  57. e.printStackTrace();
  58. }finally{
  59. mydb.close();
  60. }
  61. }
  62.  
  63. private void insertData(int id, String name, int score){
  64. //データベースへ1行挿入
  65.  
  66. ContentValues values = new ContentValues();
  67. values.put("id",id);
  68. values.put("name",name);
  69. values.put("score",score);
  70.  
  71. mydb.insert("sample", null, values);
  72. }
  73.  
  74.  
  75.  
  76. private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
  77.  
  78. public MySQLiteOpenHelper(Context c) {
  79. super(c, DB, null, DB_VERSION);
  80. }
  81.  
  82. @Override
  83. public void onCreate(SQLiteDatabase db) {
  84. db.execSQL(CREATE_TABLE);
  85. }
  86.  
  87. @Override
  88. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  89. db.execSQL(DROP_TABLE);
  90. onCreate(db);
  91. }
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement