2016-11-18 5 views
1

私はインターネット上で夢中になり、答えを探しています。SQLite(アンドロイド)でデータを格納する際の問題

現在、私はSQLite DBを使用するアンドロイドアプリケーションを構築しています。 すべてがセットアップされ、サインアップされ、ログインしています。カメラを接続して写真を撮ることができ、ローカルギャラリーに保存されます。ただし、動作しないものは次のとおりです。

  • エミュレータが動作していません。私の電話でテスト。
  • DDMSが自分のDBを表示していません。
  • CPUが必要な機能VT-xまたはSVMをサポートしていません。

しかし、主なポイントは、私はSQLiteDBをどこからでも探しています。 android studioはSQLiteDBのデータベースをどこに保存しますか?

はまた、私はまた、動作しませんでした、私の携帯電話の私のローカルストレージにデータを書き込もうとしました:次のように

public class DBUtil { 

//Because of the fact that laptop refuses to let me use an emulator, data gets saved to my phone directory, folder DataAndroid 
final static String FOLDER_EXTERNAL_DIRECTORY = Environment.getExternalStorageDirectory()+ "/DataAndroid"; 

//saving the data in my local data folder 
public static void copyDatabaseToExtStg(Context context){ 

    //external storage file 
    File externalDirectory = new File(FOLDER_EXTERNAL_DIRECTORY); 

    if(!externalDirectory.exists()) 
     externalDirectory.mkdirs(); 
    File toFile = new File(externalDirectory, DBHelper.DATABASE_NAME); 
    //internal storage file 
    File fromFile = context.getDatabasePath(DBHelper.DATABASE_NAME); 
    if (fromFile.exists()) 
     copy(fromFile, toFile); 
} 

//copy from making the file to 'writing' the file 
static void copy(File fromFile, File toFile) { 
    try { 
     FileInputStream is = new FileInputStream(fromFile); 
     FileChannel src = is.getChannel(); 
     FileOutputStream os = new FileOutputStream(toFile); 
     FileChannel dst = os.getChannel(); 
     dst.transferFrom(src, 0, src.size()); 
     src.close(); is.close(); 
     dst.close(); os.close(); 
    } catch (Exception e) { 
    } 

} }

マイDBHelperクラスになります。

public class DBHelper extends SQLiteOpenHelper { 

//db attr. 
SQLiteDatabase DataBase; 

//confirm password doesn't need to be stored, it is just a small check up 
private static final int DATABASE_VERSION = 1; 
public static final String DATABASE_NAME = "contacts.db"; 
private static final String TABLE_NAME = "contacts"; 
private static final String COLUMN_ID = "id"; 
private static final String COLUMN_NAME = "name"; 
private static final String COLUMN_SURNAME = "surname"; 
private static final String COLUMN_EMAIL = "email"; 
private static final String COLUMN_USERNAME = "username"; 
private static final String COLUMN_PASSWORD = "password"; 
//constructor 
public DBHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

//create table +TABLE_NAME 
private static final String TABLE_CREATE = "create table contacts(id integer primary key not null , " + 
     "name text not null, surname text not null, email text not null, username text not null, password text not null);"; 

@Override 
public void onCreate(SQLiteDatabase DataBase) { 
    DataBase.execSQL(TABLE_CREATE); 
    this.DataBase = DataBase; 
} 

public void insertContact(Contact contact){ 

    //To be able to insert anything in DB, it should be writeable. 
    //Because of the fact that password.equals(confirmpassword), COLUMN_CONFPASS. is missing in list below 
    DataBase = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 

    String query = "SELECT * FROM contacts"; 
    Cursor cursor = DataBase.rawQuery(query, null); 
    int count = cursor.getCount(); 

    //To make sure that the count increments, istead of assigning same number to different numbers. 
    values.put(COLUMN_ID, count); 
    values.put(COLUMN_NAME, contact.getName()); 
    values.put(COLUMN_SURNAME, contact.getSurname()); 
    values.put(COLUMN_EMAIL, contact.getEmail()); 
    values.put(COLUMN_USERNAME, contact.getUsername()); 
    values.put(COLUMN_PASSWORD, contact.getPassword()); 

    //Insert contact object into DB 
    DataBase.insert(TABLE_NAME, null, values); 
    DataBase.close(); 
} 

//Keeping DB up to date 
@Override 
public void onUpgrade(SQLiteDatabase DataBase, int CurrentVersionOld, int NewVersion) { 
    String query = "DROP TABLE IF EXISTS "+TABLE_NAME; 
    DataBase.execSQL(query); 
    this.onConfigure(DataBase); 

} 

//Read the DB 
public String searchPassword(String username) { 

    DataBase = this.getReadableDatabase(); 
    String query = "SELECT username, password FROM " + TABLE_NAME; 
    Cursor cursor = DataBase.rawQuery(query, null); 
    String uname, pw; 


    //If the password is not found, then print: 
    pw = "not found"; 

    if (cursor.moveToFirst()) { 

     do { 
      //assign index to username(0) and password (1) 
      uname = cursor.getString(0); 
      pw = cursor.getString(1); 

      if (uname.equals(username)) { 
       pw = cursor.getString(1); 
       break; 
      } 
     } while (cursor.moveToNext()); 

    } return pw; 
} 

}

誰かが私を助けてくださいことはできますか? 私のコーディングはあまり良くありません。私に同行してください:)(これには新しい)

答えて

1

SQLiteDBのデータベースはどこにありますか?

あなたがエミュレータで実行している場合は、このリンクは、あなたが電話を使用している場合は、Google PlayストアからSQLite Managerをダウンロードすることができます

https://stackoverflow.com/a/28339178/5156075

に役立つことがあります。

ここにリンクhttps://play.google.com/store/apps/details?id=com.xuecs.sqlitemanager&hl=en

+1

ありがとう! SQLite Managerを使用します。これについてのヒント?リダイレクトする必要がありますか、またはアプリケーションを携帯電話で実行すると接続されますか? – lilienfa

+1

既にダウンロードして携帯電話にインストールしているので、携帯電話と接続します。 –

+0

はい、それは動作します。ありがとうございました! – lilienfa

関連する問題