将数据从txt文件导入到android数据库

时间:2014-07-21 08:31:28

标签: java android mysql sql

我向你求助,因为很长一段时间我都试图找到这段代码的解决方案...

我有一个这样的gps音轨。

WGS 84;
U  1;
C  255,0,0,2,-1.0;
L  -02:00:00
V  0.0 0.0 0 0 0 0 0.0 -1.0 0
E 0|1|00-NUL-00 00:00:00|00:00:00|0|2984
i I00
z  12.716222,42.410293,12.736302,42.449607
T  A 42.41060087ºN 12.72081354ºE 25-SEP-08 07:13:34 N 841.8 0.0 0.0 0.0 0 -1000.0 9999999562023526400000000.0 -1 -1.0 -1.0
T  A 42.41060045ºN 12.72082259ºE 25-SEP-08 07:13:39 s 842.7 0.0 0.0 0.0 0 -1000.0 9999999562023526400000000.0 -1 -1.0 -1.0
T  A 42.41060296ºN 12.72083248ºE 25-SEP-08 07:13:44 s 842.3 0.0 0.0 0.0 0 -1000.0 9999999562023526400000000.0 -1 -1.0 -1.0 `

我需要将此坐标...导入此格式的数据库......

{
  TracksDB tracksDB = new TracksDB(context);
  TrackPointsDB trackPointsDB = new TrackPointsDB(context);
  tracksDB.open();
  trackPointsDB.open();

  File file = new File(path);
  ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));

  int count = 0;

  try
  {
     Track track;
     while ((track = (Track) in.readObject()) != null)
     {
        long id = tracksDB.insert(track.getName());

        ArrayList<TrackPoint> points = track.getTrackPoints();
        for (TrackPoint point : points)
        {
           trackPointsDB.insert(id, point.getLongitude(), point.getLatitude(), point.getAltitude(), point.getAccuracy(),
                 Long.toString(point.getTime()));
        }

        count++;
     }
  }
  catch (EOFException eof)
  {

  }

  // !// finally?
  in.close();
  tracksDB.close();
  trackPointsDB.close();

  return count;
}

我尝试打开一个跟踪文件,但我不知道如何使用相同的导入程序创建数据库..请帮助!感谢

1 个答案:

答案 0 :(得分:0)

在调用insert方法之前,调用一个创建数据库的方法。

代表。 -

public void createDatabase(){

  Class.forName("com.mysql.jdbc.Driver");

  Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

  Statement stmt = conn.createStatement();

  String sql = "CREATE DATABASE Tracks";

  stmt.executeUpdate(sql);

}

在此之后创建表格的方式相同。