Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sample;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.os.Bundle;
- import android.widget.TextView;
- public class MainActivity extends Activity{
- static final String DB = "sample.db";
- static final int DB_VERSION = 1;
- static final String CREATE_TABLE = "create table sample ( id integer primary key, name string, score integer);";
- static final String DROP_TABLE = "drop table sample;";
- static private SQLiteDatabase mydb;
- TextView tv1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv1 = (TextView)findViewById(R.id.textView1);
- int ID;
- String NAME;
- int SCORE;
- String[] array;
- String s;
- InputStream ip1;
- MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(getApplicationContext());
- try {
- ip1 = getResources().getAssets().open("SERVICE_DATE.csv");
- BufferedReader bfr1 = new BufferedReader(new InputStreamReader(ip1));
- mydb = hlpr.getWritableDatabase();
- while ((s = bfr1.readLine()) != null){
- array = s.split( "," );
- ID = Integer.parseInt( array[0] );
- NAME = array[1];
- SCORE = Integer.parseInt( array[2] );
- tv1.append(array[0] + "\t" + array[1] + "\t" + array[2] + "\n");
- insertData(ID,NAME,SCORE);
- }
- } catch (IOException e) {
- // TODO 自動生成された catch ブロック
- e.printStackTrace();
- }finally{
- mydb.close();
- }
- }
- private void insertData(int id, String name, int score){
- //データベースへ1行挿入
- ContentValues values = new ContentValues();
- values.put("id",id);
- values.put("name",name);
- values.put("score",score);
- mydb.insert("sample", null, values);
- }
- private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
- public MySQLiteOpenHelper(Context c) {
- super(c, DB, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(DROP_TABLE);
- onCreate(db);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement