편집 텍스 트에서 얻은 일부 이름으로 채워진 열이 1 개인 sqlite 데이터베이스가 있습니다. 자동 완성에 전원을 공급하는 데 사용하고 있습니다. 필자는 필자의 자동 완성에서 중복을 얻을 수 없도록 해당 열에 대해 데이터베이스를 고유하게 설정했습니다. 문제는 다음과 같습니다. 누군가가 복제본을 입력하면 데이터베이스에 해당 이름의 카운터를 만듭니다. "치즈"를 5 회 입력하면 데이터베이스의 "치즈"에 5가 입력됩니다 다른 열에. 내가 이것을 달성하기 위해 가장 자주 사용하는 자동 완성을 보여주고 일종의 "최고 사용 이름"목록보기 또는 다른 것을 가지고 있습니다. 발췌 문장으로 나를 도울 수 있습니까? DB에 복제본을 입력하고 카운터를 늘리려고 할 때 어떻게 감지 할 수 있습니까? 감사합니다. 지금까지 내 코드가 있습니다.SQLite : 중복 인 경우 증가 카운터
public class SQLiteCountryAssistant extends SQLiteOpenHelper
{
private static final String DB_NAME = "usingsqlite.db";
private static final int DB_VERSION_NUMBER = 1;
private static final String DB_TABLE_NAME = "countries";
private static final String DB_COLUMN_1_NAME = "country_name";
private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME +
" (_id integer primary key autoincrement, country_name text UNIQUE);)";
private SQLiteDatabase sqliteDBInstance = null;
public SQLiteCountryAssistant(Context context)
{
super(context, DB_NAME, null, DB_VERSION_NUMBER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO: Implement onUpgrade
}
@Override
public void onCreate(SQLiteDatabase sqliteDBInstance)
{
Log.i("onCreate", "Creating the database...");
sqliteDBInstance.execSQL(DB_CREATE_SCRIPT);
}
public void openDB() throws SQLException
{
Log.i("openDB", "Checking sqliteDBInstance...");
if(this.sqliteDBInstance == null)
{
Log.i("openDB", "Creating sqliteDBInstance...");
this.sqliteDBInstance = this.getWritableDatabase();
}
}
public void closeDB()
{
if(this.sqliteDBInstance != null)
{
if(this.sqliteDBInstance.isOpen())
this.sqliteDBInstance.close();
}
}
public long insertCountry(String countryName)
{
ContentValues contentValues = new ContentValues();
contentValues.put(DB_COLUMN_1_NAME, countryName);
return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues);
}
public boolean removeCountry(String countryName)
{
int result = this.sqliteDBInstance.delete(DB_TABLE_NAME, "country_name='" + countryName + "'", null);
if(result > 0)
return true;
else
return false;
}
public long updateCountry(String oldCountryName, String newCountryName)
{
ContentValues contentValues = new ContentValues();
contentValues.put(DB_COLUMN_1_NAME, newCountryName);
return this.sqliteDBInstance.update(DB_TABLE_NAME, contentValues, "country_name='" + oldCountryName + "'", null);
}
public String[] getAllCountries()
{
Cursor cursor = this.sqliteDBInstance.query(DB_TABLE_NAME, new String[] {DB_COLUMN_1_NAME}, null, null, null, null, DB_COLUMN_1_NAME + " ASC");
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
}
의 SQLite는 동일합니다 (사회학, 그래서 난이와 "같은"초보자이야 왜, 내가 프로그래밍 배경이없는 이해하게 될 것입니다). 내가보기에, 이것은 지금까지 안드로이드 sqlite에 관해서 배웠던 어떤 것도 닮지 않았습니다. 왜죠? 이것은 sqlite의 다른 종류입니까? 내 프로젝트에서 이것을 어떻게 구현합니까? 이 바보 같은 질문에 대해 유감스럽게 생각합니다. 나는 3-4 주 전에 배우기 시작했습니다. –
이런 식으로해야합니까? String sql = "INSERT 또는 IGNORE INTO 방문 VALUES ($ ip, 0)"; db.execSQL (sql); 또는 db.execSQL (// 여기에 직접 명령); ? 미안해. 내가 틀렸어. 처음부터 시작해서 모든 작은 세부 사항을 연구해야 할 때 이해하기가 정말 힘들다. –
예제 코드는 PHP 관련 질문입니다. $ ip는 PHP 변수입니다. 당신은 그것을 당신의 가치로 바꿀 것입니다. 예를 들면 다음과 같습니다 : '국가를 삽입하거나 무시하십시오 VALUES ('America', 0); 뒤에'UPDATE countries SET Hits = Hits + 1 WHERE country = 'America'' 그런 나라가 없다면, 추가되고 히트 카운트가 0으로 설정되고 즉시 1로 업데이트됩니다. * 해당 국가가 DB에 *있는 경우 첫 번째 명령문은 무시되고 히트 카운터는 1 씩 증가합니다. 'db.execSQL (sql);과 같은 것입니다. 여기서 sql은 SQL 변수입니다. 내 안드로이드 sqlite 인터페이스에서 약간 녹슬 었어. –