Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 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 private SQLiteDatabase mydb;
  22. TextView tv1;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28.  
  29. tv1 = (TextView)findViewById(R.id.textView1);
  30.  
  31. int ID;
  32. String NAME;
  33. int SCORE;
  34. String[] array;
  35. String s;
  36. InputStream ip1;
  37.  
  38. MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(getApplicationContext());
  39.  
  40. try {
  41. ip1 = getResources().getAssets().open("SERVICE_DATE.csv");
  42. BufferedReader bfr1 = new BufferedReader(new InputStreamReader(ip1));
  43.  
  44. mydb = hlpr.getWritableDatabase();
  45.  
  46. while ((s = bfr1.readLine()) != null){
  47. array = s.split( "," );
  48. ID = Integer.parseInt( array[0] );
  49. NAME = array[1];
  50. SCORE = Integer.parseInt( array[2] );
  51. tv1.append(array[0] + "\t" + array[1] + "\t" + array[2] + "\n");
  52. insertData(ID,NAME,SCORE);
  53. }
  54. } catch (IOException e) {
  55. // TODO 自動生成された catch ブロック
  56. e.printStackTrace();
  57. }finally{
  58. mydb.close();
  59. }
  60. }
  61.  
  62. private void insertData(int id, String name, int score){
  63. //データベースへ1行挿入
  64.  
  65. ContentValues values = new ContentValues();
  66. values.put("id",id);
  67. values.put("name",name);
  68. values.put("score",score);
  69.  
  70. mydb.insert("sample", null, values);
  71. }
  72.  
  73.  
  74.  
  75. private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
  76.  
  77. public MySQLiteOpenHelper(Context c) {
  78. super(c, DB, null, DB_VERSION);
  79. }
  80.  
  81. @Override
  82. public void onCreate(SQLiteDatabase db) {
  83. db.execSQL(CREATE_TABLE);
  84. }
  85.  
  86. @Override
  87. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  88.  
  89. }
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement