我有一个数据库类,我试图制作3个表,所有tableso都完美地创建但我无法确定为什么没有在第三个表中插入记录。它给了我错误。我在这里发布我的代码。请帮我确定一下是什么问题?
Database.java
package com.example.nearbyplaces;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String db_name = "nearby_place";
private static final int db_version = 2;
//tables
private static final String table_placetypes = "placetypes";
private static final String table_places = "table_places";
private static final String table_placeInfo = "table_placeinfo";
//column names
private static final String type_id = "type_id";
private static final String type_name = "type_name";
private static final String place_id = "place_id";
private static final String place_name = "place_name";
private static final String place_address = "place_address";
private static final String place_contact = "place_contact";
private static final String info_id = "info_id";
public DatabaseHandler(Context context) {
super(context, db_name, null, db_version);
// TODO Auto-generated constructor stub
}
// create table queries
String create_table_placetypes = "CREATE TABLE IF NOT EXISTS " + table_placetypes + "("
+ type_id + " INTEGER PRIMARY KEY NOT NULL," + type_name + " TEXT" + ")";
String create_table_places = "CREATE TABLE IF NOT EXISTS table_places (place_id INTEGER PRIMARY KEY NOT NULL, place_name TEXT, type_id INTEGER, FOREIGN KEY (type_id) REFERENCES table_placetypes(type_id))";
String create_table_placeinfo = "CREATE TABLE IF NOT EXISTS table_placeinfo (info_id INTEGER PRIMARY KEY NOT NULL, place_address TEXT, place_contact TEXT, place_id INTEGER, FOREIGN KEY (place_id) REFERENCES table_places(place_id) )";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(create_table_placetypes);
Log.d("creating", "placetypes created");
db.execSQL(create_table_places);
Log.d("creating", "places created");
db.execSQL(create_table_placeinfo);
Log.d("creating", "place_info created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + table_placetypes);
db.execSQL("DROP TABLE IF EXISTS " + table_places);
db.execSQL("DROP TABLE IF EXISTS " + create_table_placeinfo);
onCreate(db);
}
// add placetypes
void addplacetypes (placetypes pt) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(type_name, pt.getTypename());
db.insert(table_placetypes, null, values);
db.close();
}
// Getting single placetypes
placetypes getPlacetypes(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(table_placetypes, new String[] {type_id,
type_name }, type_id + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
placetypes pt = new placetypes(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return pt;
}
// Getting All placetypes
public List<placetypes> getAllPlacetypes() {
List<placetypes> placetypesList = new ArrayList<placetypes>();
// Select All Query
String selectQuery = "SELECT * FROM " + table_placetypes;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
placetypes pt = new placetypes();
pt.setTypeid(Integer.parseInt(cursor.getString(0)));
pt.setTypename(cursor.getString(1));
//String name = cursor.getString(1);
//MainActivity.ArrayofName.add(name);
// Adding contact to list
placetypesList.add(pt);
} while (cursor.moveToNext());
}
// return placetype list
return placetypesList;
}
// Getting placetypes Count
public int getPlacetypesCount() {
String countQuery = "SELECT * FROM " + table_placetypes;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public void addplaces(places p) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(place_name, p.getPlace_name());
values.put(type_id, p.getT_id());
Log.d("Type ID", String.valueOf(p.getT_id()));
db.insert(table_places, null, values);
db.close();
}
places getPlaces(int pid) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_places, new String[] {place_id, place_name,type_id}, place_id + "=?", new String[] { String.valueOf(pid) } , null, null, null, null);
if(cursor != null)
cursor.moveToFirst();
places p = new places(Integer.parseInt(cursor.getString(0)),
Integer.parseInt(cursor.getString(1)),
cursor.getString(2)
);
cursor.close();
return p;
}
public List<places> getAllPlaces(String typeName) {
List<places> placeList = new ArrayList<places>();
//String selectQuery = "SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id ";
//String selectQuery = "SELECT * FROM table_places WHERE table_places.type_id="+Integer.toString(typeid);
//String selectQuery ="SELECT * FROM table_places WHERE table_places.place_name = " +typeName+ "INNER JOIN placetypes ON placetypes.type_id = table_places.type_id";
//String selectQuery = "SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id = table_places.type_id WHERE table_places.place_name = " + typeName + "";
//String selectQuery = "SELECT * FROM " +table_places;
String selectQuery ="SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id WHERE placetypes.type_name='"+typeName+"'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst() )
{
do{
places p = new places();
/*p.setT_id(cursor.getColumnIndex(type_id));
p.setPlace_id(cursor.getColumnIndex(place_id));
p.setPlace_name(cursor.getColumnIndex(place_name));
*/
p.setPlace_id(cursor.getInt(0));
p.setPlace_name(cursor.getString(1));
p.setT_id(cursor.getInt(2));
/*String t_id = cursor.getString(4);
String p_name = cursor.getString(2);
String p_address = cursor.getString(3);
String p_contact = cursor.getString(1);*/
placeList.add(p);
}while(cursor.moveToNext());
}
cursor.close();
return placeList;
}
public int getPlaceCount () {
String selectQuery = "SELECT * FROM " +table_places;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.close();
return cursor.getCount();
}
public void addPlaceInfo(placeInfo pi) {
ContentValues values = new ContentValues();
SQLiteDatabase db = getWritableDatabase();
values.put(place_address,pi.getAddress());
values.put(place_contact, pi.getContact());
values.put(place_id, pi.getPlace_id());
Log.d("Place ID", String.valueOf(pi.getPlace_id()));
db.insert(create_table_placeinfo, null, values);
db.close();
}
placeInfo getPlaceInfo (int infoid) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(create_table_placeinfo, new String[] {info_id, place_address, place_contact, place_id}, info_id + "=?", new String[] {String.valueOf(infoid)}, null, null, null, null);
if(cursor != null)
cursor.moveToFirst();
placeInfo pi = new placeInfo(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
Integer.parseInt(cursor.getString(3)));
cursor.close();
return pi;
}
public List<placeInfo> getAllPlaceInfo(String place_name) {
List<placeInfo> placeInfoList = new ArrayList<placeInfo>();
String SelectQuery = "SELECT * FROM table_placeinfo INNER JOIN table_places ON table_place.place_id=table_placeinfo.place_id WHERE table_places.place_id= '"+place_name+"'";
SQLiteDatabase db= getWritableDatabase();
Cursor cursor = db.rawQuery(create_table_placeinfo, null);
if(cursor.moveToFirst()){
do {
placeInfo pi = new placeInfo();
pi.setId(cursor.getInt(0));
pi.setAddress(cursor.getString(1));
pi.setContact(cursor.getString(2));
pi.setPlace_id(cursor.getInt(3));
placeInfoList.add(pi);
}while(cursor.moveToNext());
}
cursor.close();
return placeInfoList;
}
public int getPlaceInfoCount () {
String selectQuery = "SELECT * FROM " +table_placeInfo;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.close();
return cursor.getCount();
}
}
MainActivity3.java
package com.example.nearbyplaces;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity3 extends Activity {
private TextView textView;
public static ArrayList<String> ArrayofName = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent check = getIntent();
String x = check.getStringExtra(MainActivity2.PLACE);
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
DatabaseHandler db = new DatabaseHandler(this);
if(db.getAllPlaceInfo(x).isEmpty()) {
Log.d("Insert: ", "Inserting ..");
db.addPlaceInfo(new placeInfo("QM Building, Plot# BC 15, Block, Khayaban-e-Roomi, Clifton", "111-241-241",1));
Log.d("Reading: ", "Reading all places..");
if(ArrayofName.isEmpty()) {
List<placeInfo> placeInfo = db.getAllPlaceInfo(x);
for(placeInfo pi : placeInfo)
{
String log = "Id: " + pi.getPlace_id() + "Address: " + pi.getAddress() + "COntact: " + pi.getContact();
//Writing Places to log
Log.d("Address: ", log);
Log.d("Contact: ", log);
System.out.println(log);
ArrayofName.add(pi.getAddress());
ArrayofName.add(pi.getContact());
textView = (TextView) findViewById(R.id.textView2);
textView = (TextView) findViewById(R.id.textView4);
}
}
}
}
}
logcat的
10-12 08:14:44.061: D/Insert:(5401): Inserting ..
10-12 08:14:44.081: D/Place ID(5401): 1
10-12 08:14:44.081: E/SQLiteLog(5401): (1) near "CREATE": syntax error
10-12 08:14:44.691: E/SQLiteDatabase(5401): Error inserting place_id=1 place_address=QM Building, Plot# BC 15, Block, Khayaban-e-Roomi, Clifton place_contact=111-241-241
10-12 08:14:44.691: E/SQLiteDatabase(5401): android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: INSERT INTO CREATE TABLE IF NOT EXISTS table_placeinfo (info_id INTEGER PRIMARY KEY NOT NULL, place_address TEXT, place_contact TEXT, place_id INTEGER, FOREIGN KEY (place_id) REFERENCES table_places(place_id) )(place_id,place_address,place_contact) VALUES (?,?,?)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at com.example.nearbyplaces.DatabaseHandler.addPlaceInfo(DatabaseHandler.java:235)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at com.example.nearbyplaces.MainActivity3.onCreate(MainActivity3.java:37)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.Activity.performCreate(Activity.java:5133)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.os.Looper.loop(Looper.java:137)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at java.lang.reflect.Method.invoke(Method.java:525)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 08:14:44.691: E/SQLiteDatabase(5401): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
方法addPlaceInfo(placeInfo pi)
,
db.insert(create_table_placeinfo, null, values);
//正在使用创建查询
应该是
db.insert(table_placeInfo, null, values);
//使用表名
答案 1 :(得分:0)
你的函数addplaceinfo有
values.put(place_address,pi.getAddress());
values.put(place_contact, pi.getContact());
values.put(place_id, pi.getPlace_id());
从主要活动中插入值
db.addPlaceInfo(new placeInfo("QM Building, Plot# BC 15, Block, Khayaban-e-Roomi, Clifton", "111-241-241",1));