나는 Android 응용 프로그램에서 작업 중입니다. 그것은 SQLite 데이터베이스를 포함합니다. 하나의 데이터베이스에 5 개의 테이블을 생성하고 5 개의 DBAdapter를 생성했습니다.SQLite 데이터베이스를 사용하여 데이터베이스 테이블과 열을 함께 연결하는 방법
테이블 (buddiesList, titles, event, 좋아요 & 싫어함) 및 외래 키 4 개 (제목, 이벤트, 좋아요 & 싫어함)의 기본 키가 있습니다. 테이블은 SQLite 데이터베이스에서 생성됩니다.
아, 하나의 DBAdapter는 5 개의 테이블을 생성하기 위해 코딩되었고 테이블을 함께 연결해야하며 기본 키 & 외래 키를 함께 연결해야합니다. 다른 4 개의 DBAdapter는 각각 5 개의 테이블에 해당합니다. 나는 코드를 게시하고 내가 말하는 것과 내가 필요한 것을 얻을 수 있습니다.
AnniversaryDBAdapter - 5 개의 테이블이 생성되었습니다.
public class AnniversaryDBAdapter
{
private static final String DATABASE_NAME = "Tables";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_DISLIKES = "create table dislikes(dlike_id integer primary key autoincrement, dislike text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_EVENTS = "create table event(event_id integer primary key autoincrement, title_id integer not null, location text not null, starttime text not null, endtime text not null, desc text not null, date text not null, name_id integer not null, FOREIGN KEY(title_id) REFERENCES titles(title_id));";
private final Context context;
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
protected SQLiteDatabase db;
private static String DB_PATH = "/data/data/main.page/Tables/";
public AnniversaryDBAdapter(Context aContext)
{
this.context = aContext;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
/*try
{
database.execSQL(CREATE_TABLE_BUDDIESLIST);
database.execSQL(CREATE_TABLE_LIKES);
database.execSQL(CREATE_TABLE_EVENTS);
database.execSQL(CREATE_TABLE_TITLE);
database.execSQL(CREATE_TABLE_DISLIKES);
}catch(SQLException e)
{
e.printStackTrace();
}*/
db.execSQL(CREATE_TABLE_BUDDIESLIST);
db.execSQL(CREATE_TABLE_LIKES);
db.execSQL(CREATE_TABLE_EVENTS);
db.execSQL(CREATE_TABLE_TITLE);
db.execSQL(CREATE_TABLE_DISLIKES);
/* database.execSQL("CREATE TRIGGER fk_budevent_nameid" +
"BEFORE INSERT" +
"ON events FOR EACH ROW BEGIN" +
"SELECT CASE WHEN((SELECT name_id FROM buddiesList WHERE name_id = new.name_id) IS NULL)" +
"THEN RAISE(ABORT, 'Foreign Key Violation')END;" +
"END;");
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
onCreate(db);
}
}
public AnniversaryDBAdapter open() throws SQLException
{
db = this.DBHelper.getWritableDatabase();
/* if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}*/
return this;
}
public void close()
{
DBHelper.close();
}
/*
public long insertEvent(String title,String location,String starttime,String endtime,String desc,String date, String name)
{
ContentValues cValues = new ContentValues();
cValues.put(KEY_TITLE, title);
cValues.put(KEY_LOCATION, location);
cValues.put(KEY_START, starttime);
cValues.put(KEY_END, endtime);
cValues.put(KEY_DESC, desc);
cValues.put(KEY_ALARM, alarm);
cValues.put(KEY_DATE, date);
cValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_EVENTS, null, cValues);
}
public boolean deleteEvent(long rowId)
{
return db.delete(CREATE_TABLE_EVENTS, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllEvents()
{
return db.query(CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, null, null, null, null, null);
}
public Cursor getEvent(long rowId) throws SQLException
{
Cursor c = db.query(true,CREATE_TABLE_EVENTS, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END, KEY_DESC, KEY_DATE, KEY_NAME}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
*/
}
당신이 AnniversaryDBAdapter에서 볼 수 있듯이
는 buddiesList 테이블에서 name_id.error가이 이벤트에 링크 좋아하는 테이블을 싫어하는 기본 키입니다. 이벤트의 name_id, likes and dislikes 테이블은 buddiesList 테이블에서 검색되는 외래 키입니다. 그러나 이벤트 페이지의 이벤트 세부 정보를 입력하고 이벤트 테이블에 저장하면 테이블의 name_id는 buddiesList 테이블에서 검색되는 숫자 여야하지만 대신 id가 아닌 이름으로 저장됩니다. 좋아요와 싫어요도 마찬가지입니다.더하기, 주석을 제거하면 if(!db.isReadOnly()) { db.execSQL("PRAGMA foreign_keys = ON;"); }
정보, 이벤트를 열 수 없습니다. & SMS 페이지, 강제 종료 메시지가 나타납니다.
BuddyDBAdapter - BuddyDBAdapter에서 buddiesList 테이블
public class BuddyDBAdapter extends AnniversaryDBAdapter
{
public static final String KEY_ROWID = "name_id";
public static final String KEY_NAME = "name";
private static final String TAG = "DBAdapter";
private static final String CREATE_TABLE_BUDDIESLIST = "buddiesList";
//private SQLiteDatabase db;
public BuddyDBAdapter(Context aContext)
{
super(aContext);
}
public long insertNames(String name)
{
ContentValues buddyValues = new ContentValues();
buddyValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_BUDDIESLIST, null, buddyValues);
}
public boolean deleteNames(long rowId)
{
return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllNames()
{
return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
}
public Cursor getNames(long rowId) throws SQLException
{
Cursor c = db.query(true, CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
}
코드가 CalendarAdapter (이벤트 테이블), LikesDBAdapter 유사하다 (좋아하는 테이블) & DislikesDBAdapter는 테이블의 제외 (테이블을 싫어하는)과 컬럼의 이름
제 문제는 테이블을 연결하는 방법을 모르겠다는 것입니다. 그리고 SQLite 데이터베이스에서 외래 키를 사용하는 방법.
내가
private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_DISLIKES = "create table dislikes(dlike_id integer primary key autoincrement, dislike text not null, name_id integer not null FOREIGN KEY(name_id) REFERENCES buddiesList(name_id));";
private static final String CREATE_TABLE_EVENTS = "create table event(event_id integer primary key autoincrement, title_id integer not null, location text not null, starttime text not null, endtime text not null, desc text not null, date text not null, name_id integer not null, FOREIGN KEY(name_id) REFERENCES buddiesList(name_id), FOREIGN KEY(title_id) REFERENCES titles(title_id));";
및
if(!db.isReadOnly())
{
db.execSQL("PRAGMA foreign_keys = ON;");
}
같이 입력해도 나는 아직도 힘 가까이 오류가 있습니다.
또한 buddiesList 테이블의 name_id를 이벤트에 연결하는 방법을 잘 모르겠습니다. & 개의 싫어하는 테이블이 있습니다. 제목 테이블의 title_id를 이벤트 테이블의 title_id에 연결합니다.
누구든지 나를 도와 줄 수 있습니까? 제공된 모든 도움을 주시면 감사하겠습니다.
오류에 대해'logcat' 출력을 보여 주면 도움이 될 것입니다. –