अरे, मैं AUTO_INCREMENT कॉलम के साथ डेटाबेस बनाना चाहता हूं। लेकिन मुझे नहीं पता कि विधि सम्मिलन में मूल्य का विश्लेषण कैसे करें। मुझे नहीं पता कि ऑटोऑक्रिकमेंट तर्क के लिए क्या पार्स करना है, और मैंने 1 को पार्स किया है जहां auto_increment होना चाहिए, लेकिन मुझे नहीं पता कि मुझे पार्स करना चाहिए।Android SQLite डेटाबेस पर AUTO_INCREMENT कैसे बनाएं?
यहां CallDatHelper.java कक्षा है जहां मैं विधि सम्मिलित करता हूं, और वह तरीका जो डेटाबेस बनाता है।
package com.psyhclo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class CallDataHelper {
private static final String DATABASE_NAME = "calls.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "contact_data";
private Context context;
private SQLiteDatabase db;
public CallDataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
}
public boolean insert(Integer cId, String cName, String numType,
String cNum, String dur, String date, String currTime) {
this.db.execSQL("insert into "
+ TABLE_NAME
+ "(id, contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
+ "values(? ," + cId + ", " + cName + ", " + numType + ", "
+ cNum + ", " + dur + ", " + date + ", " + currTime + ", ?)");
return true;
}
public void atualiza(String word) {
this.db.execSQL("UPDATE words SET cont = cont + 1 WHERE (word= ?)",
new String[] { word });
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public boolean select(String wrd) {
String word;
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
"word like ?", new String[] { wrd }, null, null, null);
if (cursor.moveToFirst()) {
do {
word = cursor.getString(0);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
return true;
} else {
return false;
}
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
null, null, null, null, "cont desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0).toUpperCase());
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, date DATE, current_time TIME, cont INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("RatedCalls Database",
"Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
और यहाँ है, जहां मैं डालने विधि डेटा मैं सम्मिलित करना चाहते हैं पार्स करने कहते हैं।
this.dh.insert(1 , 1, contactName, numType, contactNumber,
duration, callDate, currTime);
मुझे एक सुरक्षा टिप्पणी यहाँ करते हैं: मैन्युअल रूप से निर्माण एसक्यूएल इस तरह के प्रश्नों एक निश्चित नुस्खा बहुत मुश्किल हैक हो जाते है। यदि कोई हमलावर उन तर्कों में से किसी के मूल्य का निर्धारण करने के लिए मिलता है तो क्वेरी पूरी तरह से संशोधित की जा सकती है। यहां अधिक जानकारी देखें: http://en.wikipedia.org/wiki/SQL_injection – MarioVilas