2017-04-16 4 views
0

이 코드는 하나의 테이블에서 작동했습니다. .... 그리고 파일을 첨부했습니다. 이제 두 개의 테이블을 만들려고합니다. 첫 번째 작업은 table1에 데이터를 삽입하는 중입니다 ... 두 번째 작업은 삽입되지 않습니다. 내 데이터베이스 클래스 I 저장할Sqlite가 두 번째 테이블에 데이터를 삽입하지 않음

public class DatabaseHelper extends SQLiteOpenHelper { 
public static final String DATABASE_NAME="GEOTAG.db"; 
public static final String TABLE_NAME1="Coordinates_table"; 
public static final String TABLE_NAME2="Form1_table"; 
public static final String CoL_1="ID"; 
public static final String CoL_2="LATITUDE"; 
public static final String CoL_3="LONGITUDE"; 
public static final String CoL_4="IDForm1"; 
public static final String CoL_5="Residence"; 
public static final String CoL_6="Gender"; 
public static final int DATABASE_VERSION = 3; 
public static Context c; 
Intent intent =null,chooser=null; 
File file =null; 
String myFile="Data"; 
String fpath; 
String fileName; 
FileOutputStream fileOutputStream=null; 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    c=context; 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    try { 
     db.execSQL("CREATE TABLE " + TABLE_NAME1 + " (" + CoL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CoL_2 +" TEXT,"+ CoL_3 +" TEXT)"); 
     db.execSQL("CREATE TABLE " + TABLE_NAME2 + " (" + CoL_4 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CoL_5 +" NTEXT,"+ CoL_6 +" NTEXT)"); 
    } catch (SQLException e) { 
     Log.e("HEY","Error creating"); 
     e.printStackTrace(); 
    } 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    try{ 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2); 
    onCreate(db);} 
    catch (SQLException e){ 
     e.printStackTrace(); 
    } 

} 




public boolean insertData (String lat,String lng){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues =new ContentValues(); 
    contentValues.put(CoL_2,lat); 
    contentValues.put(CoL_3,lng); 

    long res =db.insert(TABLE_NAME1,null,contentValues); 
    db.close(); 
    return res != -1; 



} 

public boolean insertForm1 (String a,String b){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues =new ContentValues(); 
    contentValues.put(CoL_5,a); 
    contentValues.put(CoL_6,b); 


    long res =db.insert(TABLE_NAME2,null,contentValues); 
    db.close(); 
    return res != -1; 



} 


public void Attach() 
{ 
    int id ; 
    String latitude ; 
    String longitude ; 
    String residence ; 
    String gender ; 
    SQLiteDatabase db =this.getWritableDatabase(); 
    String[] columns={CoL_1, CoL_2, CoL_3,CoL_5,CoL_6}; 
    Cursor cursor1,cursor2; 
    cursor1 = db.query(TABLE_NAME1,columns,null,null,null,null,null); 
    cursor2 = db.query(TABLE_NAME2,columns,null,null,null,null,null); 

    fileName= myFile+"." +"txt"; 
    // writing file on external storage 
    try { 
     File sdCard = Environment.getExternalStorageDirectory(); 
     File dir = new File(sdCard.getAbsolutePath() + "/Files"); 
     dir.mkdirs(); 
     if (dir.isDirectory()) { 
      String[] children = dir.list(); 
      for (int i = 0; i < children.length; i++) { 
       new File(dir, children[i]).delete(); 
      } 
     } 
     file = new File(dir, fileName); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 



    try { 
     fileOutputStream = new FileOutputStream(file); 
     fpath = file.toString(); 

    while (cursor1.moveToNext()) { 
     int index1 = cursor1.getColumnIndex(CoL_1); 
     int index2 = cursor1.getColumnIndex(CoL_2); 
     int index3 = cursor1.getColumnIndex(CoL_3); 
     int index4 = cursor2.getColumnIndex(CoL_5); 
     int index5 = cursor2.getColumnIndex(CoL_6); 
     id = cursor1.getInt(index1); 
     latitude = cursor1.getString(index2); 
     longitude = cursor1.getString(index3); 
     residence = cursor2.getString(index4); 
     gender = cursor2.getString(index5); 


      String text1 = Integer.toString(id) + " "; 
      String text2 = latitude + " "; 
      String text3 = longitude + " "; 
      String text4 = residence + " "; 
      String text5 = gender + "\n"; 

      fileOutputStream.write(text1.getBytes()); 
      fileOutputStream.write(text2.getBytes()); 
      fileOutputStream.write(text3.getBytes()); 
      fileOutputStream.write(text4.getBytes()); 
      fileOutputStream.write(text5.getBytes()); 




     Toast.makeText(c,"Wait while the file is being attached", Toast.LENGTH_LONG).show(); 


    } 
     Toast.makeText(c,"Ready to Send", Toast.LENGTH_LONG).show(); 

     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     fileOutputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 





} 


public void SendData(){ 
    if(fpath!= null) { 
    File targetFile = new File(fpath); 
    Uri docUri = Uri.fromFile(targetFile); 
     intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_STREAM, docUri); 
     intent.setType("text/plain"); 
     intent.putExtra(Intent.EXTRA_TEXT, "Attached Data.txt"); 
     chooser = Intent.createChooser(intent, "Send Email...."); 
     c.startActivity(chooser); 
    } 
    else 
    { 
     Toast.makeText(c,"Please attach file before Sending", Toast.LENGTH_LONG).show(); 
    } 
} 


public void reset(){ 
    SQLiteDatabase db =this.getWritableDatabase(); 
    db.delete(TABLE_NAME1,null,null); 
    db.delete(TABLE_NAME2,null,null); 
    Toast.makeText(c,"Resetting Database", Toast.LENGTH_LONG).show(); 



} 

} 

 E/SQLiteLog: (1) 

입니다

: 아래로 데이터 .... 더 로그 메시지가없는 ........ 첨부 호출하는 동안 로그를 제공합니다 8 개의 다른 테이블에서 8 가지 다른 활동의 데이터를 읽은 다음 끝에 데이터베이스를 읽은 다음이를 보냅니다. 거친 텍스트 파일 ...이 작업을 수행하는 다른 방법이 있습니까?.

제가 이미 시도한 바에 따르면, 5 개의 cloumns가있는 하나의 테이블을 만들었습니다 ... 첫 번째 활동은 첫 번째 3 개 열에 데이터를 삽입하고 두 번째 활동은 4 번째 및 5 번째 열에 데이터를 삽입하는 중이었습니다. 두 행 만들기, 첫 번째 행에 null 값이있는 마지막 두 열이 있고 두 번째 행에 null 값이있는 첫 세 열이 있습니다 ... 한 행을 두 행이 아닌 한 행으로 만들려면

답변

0

를 작동 할 수 있습니다 좋은 지금

public boolean update(String Res, String Gen){ SQLiteDatabase db = 
    this.getWritableDatabase(); ContentValues contentValues =new 
    ContentValues(); contentValues.put(CoL_4,Res); contentValues.put(CoL_5,Gen); 
db.update(TABLE_NAME,contentValues, "Residence = ?",new String[] { "A" }); 
return true; } 
0

같은 행에 데이터를 삽입하면 삽입을 다시 호출하는 대신 행을 업데이트해야합니다. 예를 들어 대한 : - 다음

ContentValues cv = new ContentValues(); 
cv.put("Field1","value"); //These Fields should be your String values of 

하고 대신 삽입의 갱신 전화

myDB.update(TableName, cv, "_id="+id, null); 

당신이 refer this

+0

이 방법입니다 그 전 더 일찍 시도 했습니까 ?? – Shazil

+0

데이터를 입력 할 때 삽입 사용 업데이트 대신 이미 데이터가있는 행에 대해 null 값이 없도록하십시오. –

+0

시간 내 주셔서 감사합니다. – Shazil

관련 문제