Note that there are some explanatory texts on larger screens.

plurals
  1. POSave image path to sqlite database
    primarykey
    data
    text
    <p>I need to save an image path to an sqlite database. Image path would be /images/filename.jpg that resides in the resources folder.</p> <p>I can save all the other data to the database just not the image path itself. I have a page that allows a user to enter catalogue details for an image the choose a photo from the gallery.</p> <p>I just cant figure out how to save the images file path to the database so when the user navigate back to the database record they see the image as well as the info.</p> <p>At present just the information is saving NOT the image path.</p> <p>Hope this makes sense.</p> <p>Here is the code for the page I have so far:</p> <pre><code>var currentWin = Ti.UI.currentWindow; //var id = currentWin.id; function photoGalleryDidClose(item) { photoView.image = item.media; } var db = Titanium.Database.open('photos'); var sql = db.execute('SELECT * FROM photos'); var recordID = sql.fieldByName(''); var title = sql.fieldByName(''); var location = sql.fieldByName(''); var photographer = sql.fieldByName(''); var equipment = sql.fieldByName(''); var notes = sql.fieldByName(''); var date = sql.fieldByName(''); var imageUrl = sql.fieldByName(''); //Labels to display catalogue details var labelrecordID = Titanium.UI.createLabel({ text:'Record ID:', font:{fontSize:18}, top:30, left: 10, height: 45, width:'auto' }) currentWin.add(labelrecordID); var textrecordID = Titanium.UI.createTextField({ //text:' '+ title +'', hintText: 'Enter an ID Number', font:{fontSize:18}, color:'black', borderRadius: 2, top:30, left: 150, height: 45, backgroundColor:'white', width:'300' }) textrecordID.addEventListener("change", function(e) { newID = e.value; }); currentWin.add(textrecordID); var labelTitle = Titanium.UI.createLabel({ text:'Title:', font:{fontSize:18}, top:80, left: 10, height: 45, width:'auto' }) currentWin.add(labelTitle); var textTitle = Titanium.UI.createTextField({ text:' '+ title +'', hintText: 'Title', font:{fontSize:18}, color:'black', borderRadius: 2, top:80, left: 150, height: 45, backgroundColor:'white', width:'300' }) textTitle.addEventListener("change", function(e) { newTitle = e.value; }); currentWin.add(textTitle); ///// I HAVE CHOPED OUT THE MIDDLE BIT AS IT IS JUST REPETITION OF ABOVE \\\\\ var labelDate = Titanium.UI.createLabel({ text:'Date:', font:{fontSize:18}, top:330, left: 10, height: 45, width:'auto' }) currentWin.add(labelDate); var textDate = Titanium.UI.createTextField({ text:' '+ date +'', hintText: 'Date', font:{fontSize:18}, color:'black', borderRadius: 2, top: 330, left: 150, height: 45, backgroundColor:'white', width:'300' }) textDate.addEventListener("change", function(e) { newDate = e.value; }); currentWin.add(textDate); // create a view to add the label and photo var photoContainerView = Ti.UI.createView({ top: 400, left: 10, height: 230, width: Ti.UI.FILL }); var photoLabel = Ti.UI.createLabel({ text: 'Photo:', left: 0, height: Ti.UI.SIZE, width: Ti.UI.SIZE, font:{fontSize:18} }); photoContainerView.add(photoLabel); var photoView = Ti.UI.createImageView({ image: imageUrl, top: 0, left: 110, height: 200, width: 200, borderColor: 'gray', borderWidth: 1 }); photoContainerView.add(photoView); var button_photo = Titanium.UI.createButton({ title: 'Add Image', color: 'white', backgroundColor: '#464646', font:{fontSize:20,fontFamily:'Helvetica Neue'}, top: 20, right:10, width: 130, height: 60 }); button_photo.addEventListener('click', function(e){ //Titanium.Media.openPhotoGallery({allowEditing: true, success:photoGalleryDidClose}); Titanium.Media.openPhotoGallery({ success:function(event) { var image = event.media; photoView.image = image; } }); }); ///////////PROBLEM HERE SAVING IMAGE INFO??\\\\\\\\\\ button_photo.addEventListener("click", function(e) { newImage = e.value; }); photoContainerView.add(button_photo); var button_save = Titanium.UI.createButton({ title: 'Save Record', color: 'white', backgroundColor: '#464646', font:{fontSize:20,fontFamily:'Helvetica Neue'}, top: 100, right:10, width: 130, height: 60, //enabled:false }); ///Save the record - Add event listener for save button button_save.addEventListener("click", function(e) { //if (button_save.enabled) { var db = Titanium.Database.open('photos'); db.execute('INSERT INTO photos (id,title,location,photographer,equipment,notes,date,imageUrl) VALUES(?,?,?,?,?,?,?,?)',newID,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,newImage); var last = db.lastInsertRowId; //} }); photoContainerView.add(button_save); currentWin.add(photoContainerView); </code></pre> <p>Hope this makes sense</p> <p>As I said everything saves fine except the image path.</p> <p>The problem I have is just getting the image and assigning it the correct path to save it in the database</p> <p>Thanks for any assistance</p> <p>JC</p> <p>UPDATE UPDATE UPDATE</p> <p>OK this is as far as I can get and I am pretty stumped! I can get all the correct details saved and the correct image path (i think) but it still does not display the image. This is the code to chose and then save the image:</p> <pre><code>button_photo.addEventListener('click', function() { Ti.Media.openPhotoGallery({ mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO], success : function(e) { photoView.image = e.media; var time = new Date(); var name = '/images/name' + time.getTime() + '.jpg'; var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, name); f.write(e.media); var newimage = f + '/' + name; //Just trying different things. Ti.API.info('f.nativePath' + f.nativePath); var db = Titanium.Database.open('photos'); db.execute('INSERT INTO photos (id,title,location,photographer,equipment,notes,date,imageUrl) VALUES(?,?,?,?,?,?,?,?)', name,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,name); db.close(); //insertdb(newID,newTitle,newLocation,newPhotographer,newEquip,newNotes,newDate,f); //aTableView.setData(getData()); }, cancel : function() { }, error : function(err) { } }); }); </code></pre> <p>Whether I save the image path to "images/filname.png" or to "f.nativePath" it does not seem to make any difference?</p> <p>I have some pre populated field in the database and the image is stored in "images/filname.jpg" and these show up in my app fine. It is just when I try to add an image from the phone gallery that it does not show up...</p> <p>Any help would be great!</p> <p>Thanks JC</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload